这是使用ThenFetch()加载多个集合的正确方法吗? [英] Is this the right way of using ThenFetch() to load multiple collections?
问题描述
我正在尝试使用 NHibernate 3 alpha 1 .我想知道这是否是使用ThenFetch()的正确方法?
I'm trying to load all the collections eagerly, using NHibernate 3 alpha 1. I'm wondering if this the right way of using ThenFetch()?
具有复数名称的属性是集合.其他只是一个对象.
Properties with plural names are collections. The others are just a single object.
IQueryable<T> milestoneInstances = Db.Find<T, IQueryable<T>>(db =>
from mi in db
where mi.RunDate == runDate
select mi).Fetch(mi => mi.Milestone)
.ThenFetch(m => m.PrimaryOwners)
.Fetch(mi => mi.Milestone)
.ThenFetch(m => m.SecondaryOwners)
.Fetch(mi => mi.Milestone)
.ThenFetch(m => m.Predecessors)
.Fetch(mi => mi.Milestone)
.ThenFetch(m => m.Function)
.Fetch(mi => mi.Milestone)
.ThenFetchMany(m => m.Jobs)
.ThenFetch(j => j.Source)
;
我曾想过在 NHibernate论坛中询问此问题,但是很遗憾,禁止访问google网上论坛从我所在的地方.我知道 Fabio 在这里,所以也许NHibernate团队的成员可以对此有所启发? 谢谢
I thought of asking this in the NHibernate forums but unfortunately access to google groups is forbidden from where I am. I know Fabio is here, so maybe the guys from the NHibernate team can shed some light on this? Thanks
推荐答案
显然,在这种情况下,没有使用"ThenFetch
"的正确"方法.您的示例运行正常,但生成的SQL包含许多Milestone
的联接,并非如此.
Apparently, there's no "right" way to use ThenFetch
in such a case. Your example works fine but SQL produced contains many joins to Milestone
, which isn't that right.
使用IQueryOver
代替IQueryable
允许您使用Fetch中的>复杂语法:
Using IQueryOver
instead of IQueryable
allows you to use complex syntax in Fetch
:
Fetch(p => p.B)
Fetch(p => p.B.C) // if B is not a collection ... or
Fetch(p => p.B[0].C) // if B is a collection ... or
Fetch(p => p.B.First().C) // if B is an IEnumerable (using .First() extension method)
因此,您的情况应该是:
So in your case it would be:
query // = session.QueryOver<X>()
.Fetch(mi => mi.Milestone).Eager
.Fetch(mi => mi.Milestone.PrimaryOwners).Eager
.Fetch(mi => mi.Milestone.SecondaryOwners).Eager
.Fetch(mi => mi.Milestone.Predecessors).Eager
.Fetch(mi => mi.Milestone.Function).Eager
.Fetch(mi => mi.Milestone.Jobs).Eager
.Fetch(mi => mi.Milestone.Jobs.First().Source).Eager
这篇关于这是使用ThenFetch()加载多个集合的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!