Search code examples
c#nhibernatehibernatehibernate-criteria

Select all entities of exact class, but not derived from it using NHibernate Criteria API


I have two classes: Cat and DomesticCat, that extends Cat.

I want to select all Cats, but no oneDomesticCat. How to do it using NHibernate criteria API?


Solution

  • var nonDomesticCats = session.CreateCriteria<Cat>()
                                 .Add(Restrictions.Eq("class", typeof(Cat)))
                                 .List<Cat>();
    

    class is a pseudo-property that represents the concrete type of entities in a class hierarchy.

    It can be used transparently with any inheritance strategy except implicit.