0

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?

Serge S.
  • 4,855
  • 3
  • 42
  • 46

2 Answers2

2
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.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • A class name embedded in the `where` clause will be **translated to its discriminator value** - says Hibernate documentation. – Serge S. May 28 '10 at 13:01
  • It's translated to a discriminator value when using table-per-clas-hierarchy, and to the corresponding internal case-statement values when using table-per-class or table-per-concrete-class. – Diego Mijelshon May 28 '10 at 14:04
0

well it depends on the implementation.

If for example, you have a discriminator column (lets say <discriminator column="CatType" type="string"/> and the DomesticCat inheritor class discriminates with value "domestic") you could make an query like this

var allCatsButDomestic = nhSes.CreateQuery("from Cat c where c.CatType <> :catType")
     .SetString("catType", "domestic")
     .List<Cat>();

(in this particular example the Cat abstract class also maps the CatType column to a CatType string property)

EDIT and in Criteria form

var nonDomesticCats = session.CreateCriteria<Cat>()
                             .Add(Restrictions.Not(Restrictions.Eq("CatType", "domestic")))
                             .List<Cat>();

your comment about AnotherCat again implies that there is some way of discriminating between entities at the db level.

Jaguar
  • 5,929
  • 34
  • 48
  • This is HQL, and it strictly depends on hierarchy. And what about if `AnotherCat` is derived from `DomesticCat`. I need to select only `Cat`s, your query says: select all, except `DomesticCat`. – Serge S. May 28 '10 at 12:31