如何渴望在列表/集合中加载对象? [英] How to eager load objects in a list/collection?

查看:102
本文介绍了如何渴望在列表/集合中加载对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

ObjectB objectBAlias = null;
ObjectC objectCAlias = null;
var query = session.QueryOver<ObjectA>();
var results = query
    .JoinAlias(x => x.listOfBs, () => objectBAlias)
    .JoinAlias(x => objectBAlias.ObjectC, () => objectCAlias)
    .TransformUsing(new DistinctRootEntityResultTransformer())
    .List<ObjectA>();

class ObjectA
{
    IList<ObjectB> listOfBs;
}

class ObjectB
{
    ObjectA a;
    ObjectC c;
}

class ObjectC
{
    int x;        
}

ObjectA与ObjectC的关系为many-to-many,其中ObjectB为联接表. ObjectA具有ObjectB的列表,而ObjectB具有ObjectC的比例.我正在尝试加载ObjectC,但没有成功.

ObjectA has a many-to-many relationship to ObjectC with ObjectB being the joining table. ObjectA has a list of ObjectB's, and ObjectB has a proporty of ObjectC. I'm trying to eager load ObjectC but have had no success.

让所有ObjectC渴望加载的唯一方法是这样做:

The only way I've gotten all the ObjectCs to eager load was by doing this:

foreach (ObjectA a in results)
{
    foreach (var b in a.listOfBs)
    {
        NHibernateUtil.Initialize(b.c);
    }
}

但这似乎并不能很好地扩展.

But this doesn't seem like something that would scale very well.

推荐答案

我建议-不要尝试(急于加载多对多).而是-使用内置功能:

I would suggest - do not try that (eager loading of many-to-many). Instead - use built in feature:

19.1.5.使用批量提取

NHibernate可以有效地使用批处理获取,也就是说,如果访问了一个代理(或集合),NHibernate可以加载多个未初始化的代理.批处理获取是对惰性选择获取策略的优化.有两种方法可以调整批处理提取:在类和集合级别.

NHibernate can make efficient use of batch fetching, that is, NHibernate can load several uninitialized proxies if one proxy is accessed (or collections. Batch fetching is an optimization of the lazy select fetching strategy. There are two ways you can tune batch fetching: on the class and the collection level.

要获取更多详细信息,请检查以下内容:

To get more details check these:

  • How to Eager Load Associations without duplication in NHibernate?
  • NHibernate Fetch/FetchMany duplication in resultset, how to fix with ToFuture()

获取集合是一项困难的操作.它有很多副作用(如您所知,当获取更多集合时).但是即使获取了一个集合,我们仍在加载许多重复的行.

Fetching Collections is a difficult operation. It has many side effects (as you realized, when there are fetched more collections). But even with fetching one collection, we are loading many duplicated rows.

这篇关于如何渴望在列表/集合中加载对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆