如何在 list/collection 中急切加载对象?
How to eager load objects in a list/collection?
我有以下查询:
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 是连接 table。 ObjectA 有 ObjectB 的列表,而 ObjectB 有 ObjectC 的比例。我正在尝试急切加载 ObjectC 但没有成功。
我让所有 ObjectC 立即加载的唯一方法是这样做:
foreach (ObjectA a in results)
{
foreach (var b in a.listOfBs)
{
NHibernateUtil.Initialize(b.c);
}
}
但这似乎不太适合扩展。
我建议 - 不要尝试 (多对多的急切加载)。相反 - 使用内置功能:
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.
要了解更多详细信息,请检查这些:
- How to Eager Load Associations without duplication in NHibernate?
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.
我有以下查询:
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 是连接 table。 ObjectA 有 ObjectB 的列表,而 ObjectB 有 ObjectC 的比例。我正在尝试急切加载 ObjectC 但没有成功。
我让所有 ObjectC 立即加载的唯一方法是这样做:
foreach (ObjectA a in results)
{
foreach (var b in a.listOfBs)
{
NHibernateUtil.Initialize(b.c);
}
}
但这似乎不太适合扩展。
我建议 - 不要尝试 (多对多的急切加载)。相反 - 使用内置功能:
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.
要了解更多详细信息,请检查这些:
- How to Eager Load Associations without duplication in NHibernate?
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.