开发者

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

I have two cla开发者_Go百科sses: Cat and DomesticCat, that extends Cat.

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜