如何使用 NHibernate ICriteria 检索没有连接的基类对象列表?

发布于 2024-08-14 05:54:17 字数 276 浏览 6 评论 0原文

假设我有一个名为 Pet 的基类和两个继承 Pet 的子类 Cat 和 Dog。

我只是将它们映射到三个表 Pet、Cat 和 Dog,其中 Pet 表包含基类属性,Cat 和 Dog 表包含 Pet 表的外键以及特定于猫或狗的任何其他属性。联合子类策略。

现在,使用 NHibernate 和 ICriteria,我如何获得所有不同宠物作为 Pet 对象的列表,不是猫或狗对象 - 只是普通的 Pet 对象,而不与其他表进行任何联接?我只对 Pet 中包含的信息感兴趣。

Let's say I have a base class called Pet and two subclasses Cat and Dog that inherit Pet.

I simply map these to three tables Pet, Cat and Dog, where the Pet table contains the base class properties and the Cat and Dog tables contain a foreign key to the Pet table and any additional properties specific to a cat or dog. A joined subclass strategy.

Now, using NHibernate and ICriteria, how can I get a list of all the different pets as Pet objects, not Cat or Dog objects - just plain Pet objects, without making any joins to the other tables? I am only interested in the information contained in Pet.

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

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

发布评论

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

评论(6

回眸一笑 2024-08-21 05:54:17

我刚刚做了类似的事情,发现了这个 博客文章 by Ayende 非常有帮助!我采用了连接子类的方法,它很好地支持查询基类。

然而,要在不加入的情况下找到“仅宠物”,我认为您需要每类表层次结构方法。或者,您可以使用 table-per-subclass 并仅对子类进行左联接,并将其限制为在任何左联接上都没有匹配项的行。

I just did something similar and found this blog post by Ayende extremely helpful! I went with a joined-subclass approach and it nicely supports querying the base class.

However, to find "only pets" without joining I think you'd need the table-per-class hierarchy approach. Or, you could use the table-per-subclass and just do left joins on the subclasses and restrict it to rows that had no matches on any of the left joins.

作妖 2024-08-21 05:54:17

嗯,选择“作为宠物”我认为会带回所有对象(而不仅仅是未子类化的宠物)。我不知道有什么干净的方法,但我不是 NH 专家。

也许您可以直接查询父表中的鉴别器列?尽管您可能必须报告自定义 SQL 查询而不是 HQL 或 ICriteria。

Hmm, selecting 'as pet' would I think bring back all object (not just the pets that are not subclassed). I don't know of a clean way of doing it, but then I'm not an NH expert.

Maybe you could query the discriminator column in the parent table directly? Although perhaps you would have to resport to a custom SQL query rather than HQL or ICriteria.

风轻花落早 2024-08-21 05:54:17

这似乎工作正常:

我创建了一个视图,选择了 Pet 表上的所有列。然后,我创建了一个简单的 DTO 类,并将其映射到视图。之后就可以使用ICriteria了。

也许不是最漂亮或最干净的解决方案,但肯定比动态查询字符串更容易使用。

This seems to work ok:

I created a view that made selected all columns on the Pet table. I then created a simple DTO class that I mapped to the view. After that, ICriteria can be used.

Not the prettiest or cleanest solution perhaps, but certainly easier to work with than dynamic query strings.

稀香 2024-08-21 05:54:17

我不相信您可以使用 Criteria API 来做到这一点。但是,您可以使用 HQL 来做到这一点:

var hql = @"from Pet p where p.class = Pet";

I don't believe you can do this with the Criteria API. However, you can do it with HQL:

var hql = @"from Pet p where p.class = Pet";
玻璃人 2024-08-21 05:54:17

为什么不为“PetsOnly”创建一个新地图并删除您的鉴别器和其他非必填字段?我认为克里斯托弗在上面提到了同样的事情;仍然应该为您提供完整的插入更新和删除选择支持。宠物只是一个宠物...

Why not create a new map for "PetsOnly" and and remove your discriminator and other non required fields? I think Kristoffer above aluded to the same thing; should still give you full insert update and delete select support. The pet is only a Pet...

岁月打碎记忆 2024-08-21 05:54:17

对于许多情况来说已经足够了:将属性投影到对象本身中。

请小心使用,因为该对象已损坏,因为它的类型不正确

var propsToSelect = Projections.ProjectionList();
foreach (var prop in sessionfactory.GetClassMetadata(typeof(Pet)).PropertyNames)
{
    propsToSelect.Add(Projections.Property(prop.Name), prop.Name);
}
var results = session.CreateCriteria<Pet>()
    .SetProjection(propsToSelect)
    .SetResultTransformer(Transformers.AliasToBean<Pet>())
    .List<Pet>();

good enough for many situations: project the properties into the object itself.

USE WITH CARE BECAUSE THE OBJECT IS BROKEN IN THE SENSE THAT IT HAS NOT THE CORRECT TYPE

var propsToSelect = Projections.ProjectionList();
foreach (var prop in sessionfactory.GetClassMetadata(typeof(Pet)).PropertyNames)
{
    propsToSelect.Add(Projections.Property(prop.Name), prop.Name);
}
var results = session.CreateCriteria<Pet>()
    .SetProjection(propsToSelect)
    .SetResultTransformer(Transformers.AliasToBean<Pet>())
    .List<Pet>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文