预先加载的孩子和儿童的孩子集合在NHibernate的 [英] Eager loading child and child-of-child collections in NHibernate

查看:93
本文介绍了预先加载的孩子和儿童的孩子集合在NHibernate的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,NHibernate的尝试加载数据的一小层次。我的域模型如下:

I've got a problem with NHibernate trying to load a small hierarchy of data. My domain model looks like:

class GrandParent
{
    int ID{get;set;}
    IList<Parent> Parents {get; set;}
}

class Parent
{
    IList<Child> Children {get; set;}
}

class Child
{
}

和我想急于加载所有家长和孩子对于给定的祖父母。这LINQ到NH查询创建正确的SQL并加载预期的祖父母:(该示例假设祖父母有2个父母谁各有2个孩子对象 - 总让4子对象)

and I would like to eager load all parents and children for a given GrandParent. This Linq-to-NH query creates the correct SQL and loads the GrandParent as expected: (the example assumes the grandparent has 2 parents who each have 2 child objects - so 4 child objects in total).

var linq = session.Linq<GrandParent>();
linq.Expand("Parents");
linq.Expand("Parents.Children");
linq.QueryOptions.RegisterCustomAction(c => 
    c.SetResultTransformer(new DistinctRootEntityResultTransformer()));
var grandparent = (select g from session.Linq<GrandParent>()
                   where g.ID == 1
                   select g).ToList();

Assert(grandparent.Count == 1); //Works
Assert(grandparent.Parents.Count == 2); //Fails - count = 4!



grandparent.Parents集合包含4个项目,其中2个是重复的。看来DistinctRootEntityResultTransformer仅适用于集合1级深,所以父母收集被复制取决于许多小孩如何对象,每个家长都有。

The grandparent.Parents collection contains 4 items, 2 of which are duplicates. It seems the DistinctRootEntityResultTransformer only works on collections 1 level deep, so the Parents collection is duplicated depending on how many Child objects each parent has.

是否有可能获得NH到只包括不同的父对象?

Is it possible to get NH to only include the distinct Parent objects?

非常感谢。

推荐答案

如果您的映射设置为FetchType.Join,尝试将其更改为FetchType.Select。

If your mapping is set to FetchType.Join, try changing it to FetchType.Select.

这篇关于预先加载的孩子和儿童的孩子集合在NHibernate的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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