NHibernate如何使Criteria内部联接而又不使对象水化? [英] NHibernate How to make Criteria inner join without hydrating objects?
问题描述
一些快速的休眠问题:
我有sql表:
Item { Id, Name }
ItemRange { Id, Name }
ItemHasItemRange { Id, ItemId, ItemRangeId }
映射很简单,因此我不会粘贴它们,ItemId和ItemRangeId是外键,Item类具有映射为懒惰包的ItemHasItemRanges集合.
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.
我想要所有特定于ItemRange的项目,但我不想检索关联的ItemRangeObjects,我只想进行内部联接以缩小结果范围.
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.
当我这样做时:
c.CreateCriteria("Item", "i")
.CreateAlias("ItemHasItemRanges", "ihpr", JoinType.InnerJoin)
.Add(Restrictions.Eq("ihpr.ItemRange.Id", I18nHelper.CurrentItemRange.Id));
它工作正常,但是所有 ItemHasItemRange 对象也都已获取到 Item.ItemHasItemRanges 集合(映射为惰性)
It works fine, but all ItemHasItemRange objects are fetched as well to the Item.ItemHasItemRanges collections (which is mapped as lazy)
我不想获取Item.ItemHasItemRanges,因为这需要时间.我只想做内部联接来限制结果集.在NHibernate中有可能吗?
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?
推荐答案
我发现问题出在其他地方. ItemHasItemRange表在ItemId和ItemRangeId上没有多个索引-ID在每个字段上只有单独的索引.这就是为什么性能如此差的原因.
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.
但是NHibernate问题仍然有效-是否有可能为准则创建内部联接只是为了缩小结果范围,而不是获取通常是惰性的所有联接对象.
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.
这篇关于NHibernate如何使Criteria内部联接而又不使对象水化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!