NHibernate 如何在不水合对象的情况下进行 Criteria 内连接?
一些快速的nhibernate问题:
我有sql表:
Item { Id, Name }
ItemRange { Id, Name }
ItemHasItemRange { Id, ItemId, ItemRangeId }
映射很简单,所以我不会粘贴它们,ItemId和ItemRangeId是外键,Item类具有映射为惰性袋的ItemHasItemRanges集合。
我想要特定为 ItemRange 的所有项目,但我不想检索关联的 ItemRangeObjects,我只想进行内部联接以缩小结果范围。
当我这样做时:
c.CreateCriteria("Item", "i")
.CreateAlias("ItemHasItemRanges", "ihpr", JoinType.InnerJoin)
.Add(Restrictions.Eq("ihpr.ItemRange.Id", I18nHelper.CurrentItemRange.Id));
它工作正常,但所有 ItemHasItemRange 对象也会被提取到 Item.ItemHasItemRanges 集合(映射为惰性)
我不希望获取 Item.ItemHasItemRanges,因为这需要时间。我只想进行内部联接来限制结果集。在NHibernate中可以吗?
Some quick nhibernate problem:
I have sql tables:
Item { Id, Name }
ItemRange { Id, Name }
ItemHasItemRange { Id, ItemId, ItemRangeId }
Mappings are simple, so I will not paste them, the ItemId and ItemRangeId are foreign keys, Item class has ItemHasItemRanges collection mapped as lazy bag.
I want all items which are in particular ItemRange, but I do not want to retrieve associated ItemRangeObjects, I just want to do inner join to narrow results.
When I do it like that:
c.CreateCriteria("Item", "i")
.CreateAlias("ItemHasItemRanges", "ihpr", JoinType.InnerJoin)
.Add(Restrictions.Eq("ihpr.ItemRange.Id", I18nHelper.CurrentItemRange.Id));
It works fine, but all ItemHasItemRange objects are fetched as well to the Item.ItemHasItemRanges collections (which is mapped as lazy)
I do not want to fetch Item.ItemHasItemRanges, because it takes time. I just want to do inner join to limit result set. It is possible in NHibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我认为您只是想检索这些对象以显示概述/列表,并且您不会实际对这些对象“做”一些事情(除非可能加载其中之一)?
在这种情况下,我认为你最好使用“投影”来工作。
场景如下:
您必须创建一个(简单)类,其中仅包含您想要显示的属性(您感兴趣的地方)。
您必须将该类“导入”到 NHibernate,以便 NHibernate 知道它的存在。
接下来,您可以像现在一样创建 Criteria 语句。 (使用您的域类)。
然后,您应该指定投影的外观。也就是说,
Item
实体的属性如何映射到“DTO”/View 类(= 您刚刚创建的简单类)的属性。指定应使用
AliasToBean
ResultTransformer。然后,执行您的条件查询。 NHibernate 将能够生成检索所有必要数据所需的最简单的查询。
我在此处解释了类似的内容
So I think that you just want to retrieve those objects in order to show an overview / list, and you are not going to actually 'do' something with those objects (unless perhaps loading one of them) ?
In that case, I think that it is better for you to work with 'projections'.
Here's the scenario:
You'll have to create a (simple) class that just contains the properties that you want to show (where you're interested in).
You'll have to 'import' that class into NHibernate, so that NHibernate knows of its existence.
Next, you can create your Criteria statement like you have it now. (Working with your domain classes).
Then, you should specify how the projection should look like. That is, how the properties of your
Item
entity map to the properties of your 'DTO'/View class (= the simple class you just created).Specify that an
AliasToBean
ResultTransformer should be used.Then, execute your Criteria query. NHibernate will be able to produce the simplest possible query that is needed in order to retrieve all the data that is necessary.
I've explained something similar here
我发现问题出在其他地方。 ItemHasItemRange 表在 ItemId 和 ItemRangeId 上没有多个索引 - id 仅在每个字段上有单独的索引。这就是性能如此差的原因。
但 NHibernate 问题仍然有效 - 是否可以为条件创建内部联接,仅缩小结果范围,而不获取通常是惰性的所有联接对象。
I find out the problem was somewhere else. ItemHasItemRange table did not have multiple index on ItemId and ItemRangeId - id only had separate indexes on each field. Thats why performance was so poor.
But NHibernate question is still valid - is it possible to create inner join for criteria only to narrow results and not to fetch all joined objects which normally are lazy.