选择确切类的所有实体,但不是使用 NHibernate Criteria API 从该类派生的

发布于 2024-09-02 22:59:31 字数 173 浏览 2 评论 0原文

我有两个类:CatDomesticCat,它们扩展了 Cat

我想选择所有Cat,但不选择任何DomesticCat。如何使用 NHibernate 标准 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2024-09-09 22:59:31
var nonDomesticCats = session.CreateCriteria<Cat>()
                             .Add(Restrictions.Eq("class", typeof(Cat)))
                             .List<Cat>();

class 是一个伪属性,表示类层次结构中实体的具体类型。

它可以透明地与除隐式继承策略之外的任何继承策略一起使用。

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.

九歌凝 2024-09-09 22:59:31

这取决于实施。

例如,如果您有一个鉴别器列(假设 DomesticCat 继承类通过值进行区分“domestic”)您可以进行这样的查询

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

(在这个特定示例中,Cat 抽象类还将 CatType 列映射到 CatType 字符串属性)

编辑并在 Criteria 表单中

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

您对 AnotherCat 的评论再次暗示:有一些方法可以在数据库级别区分实体。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文