这是使用 thenFetch() 加载多个集合的正确方法吗?
我正在尝试使用 NHibernate 3 alpha 1 急切地加载所有集合。我想知道这是否是使用 thenFetch() 的正确方法?
具有复数名称的属性是集合。其他的只是一个单一的对象。
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'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)
;
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
显然,在这种情况下没有“正确”的方法来使用
ThenFetch
。您的示例工作正常,但生成的 SQL 包含许多与Milestone
的联接,这是不正确的。使用
IQueryOver
而不是IQueryable
允许您使用 Fetch 中的复杂语法:所以在您的情况下它将是:
Apparently, there's no "right" way to use
ThenFetch
in such a case. Your example works fine but SQL produced contains many joins toMilestone
, which isn't that right.Using
IQueryOver
instead ofIQueryable
allows you to use complex syntax inFetch
:So in your case it would be:
您缺少的一件事是您应该使用 FetchMany() ,然后 FetchMany() 是子属性是一个集合。
The one thing you are missing is that you should use FetchMany() and ThenFetchMany() is the child property is a collection.
正如 leora 所说,确保在获取子集合时使用
新的 Fetch,应该从根中获取,但这并不总是发生。有时您需要将它们创建为单独的查询或使用 Criteria Futures 来批量进行多个获取。
As leora said, make sure when fetching children collections that you use
A new Fetch, should pick up from the root, but this does not always happen. Sometimes you need to create them as separate queries or use Criteria Futures to batch up a multiple fetch.