2011-10-21 39 views
5

我有一个与<many-to-one>关联的实体,并且该实体有两个<many-to-one>,我想一次提取。我可以通过这个查询实现这一目标:ThenFetch中的多次提取

var tshead = session.Query<MainEntity>() 
       .Fetch(r=>r.FirstAssoc).ThenFetch(p=>p.Other) 
       .Fetch(r=>r.FirstAssoc).ThenFetch(p=>p.Another) 
       .Take(10) 
       .ToList(); 

正如你看到的,我不得不写两次.Fetch(r=>r.FirstAssoc) 我相信我能避免这一点,但我无法弄清楚如何。任何想法 ?

谢谢!

+1

我不认为你可以避免它,除非'Fetch(r => r.FirstAssoc.Another)'有效,我怀疑。 –

+0

@Diego谢谢,你的疑问得到证实(表情太复杂了)。但它会很高兴。 –

+0

我有这个完全相同的问题,虽然我的FirstAssoc是一个集合,所以它是FetchMany.ThenFetch.FetchMany.ThenFetch。会喜欢这个解决方案。 – mikeschuld

回答

3

如果您选择一个特定的 'MainEntity',那么您可以在使用QueryOver像这样做:

FirstAssoc firstAssoc = null; 
Other other = null; 
Another another = null; 

tshead = session.QueryOver<MainEntity>() 
       .Where(x => x.Id == id) 
       .JoinAlias(x => x.FirstAssoc,() => firstAssoc) 
       .JoinAlias(() => firstAssoc.Other,() => other) 
       .JoinAlias(() => firstAssoc.Another,() => another) 
       .SingleOrDefault(); 

我已经写在这里:

http://www.philliphaydon.com/2011/04/nhibernate-querying-relationships-are-depth/

你不能这样做:

一多对多

only One-Many-One

我不确定你可以做Many-Many-One,转换太难了。

+0

@Diego - 你可能会对CC感兴趣。 – Phill

+0

+1,但您使用的是QueryOver,我们可以通过linqtonh实现吗? PS你的博客真的很有趣:) –

+0

@Felice - 不知道你是否可以用查询做到这一点,我怀疑这是不可能的,但我会在这个周末有一个戏看看我能得到多远。任何原因您无法使用QueryOver?感谢有关我的博客的评论:)赞赏。 – Phill