2

考虑以下场景:

A 类与 B 类具有一对多关系。B 类与 C 类具有多对一关系。

class A {
  IList<B> BList {get;set;}
}
class B {
  C CMember{get;set;}
}

class C {
   //null
}

如果我使用类似的东西从数据库中加载 B 类

   IList<B> result = query.List<B>();

一切都按预期工作,

但是,如果我这样做:

   DetachedCriteria query = DetachedCriteria.For(typeof(A));
   query.CreateAlias("B", "B", JoinType.InnerJoin);
   IList<A> result = query.List<A>();

然后,NHibernate 也会从表 C 中选择并加载所有 C。

下面的映射:一个映射...

<bag name="BList " table="B" lazy="true" inverse="false">
    <key column="id" />
    <one-to-many class="B" />
    </bag>

B 映射...

<many-to-one class="C" name="CMember" column="idC" lazy="proxy" outer-join="true" />

有任何想法吗?谢谢。

4

1 回答 1

1

I have created a sample app to test the scenario you outline and I was not able to reproduce a difference between the Criteria and the DetachedCriteria, they both returned the same result for me.

Whether or not the load obeys the lazy property was dependant on the outer-join attribute, when it is set I always got C loaded eagerly, when I removed the outer-join attribute it lazy loaded C through a proxy.

So one possible solution is to change the B mapping to:

<many-to-one class="C" name="CMember" column="idC" lazy="proxy"/>
于 2011-06-15T06:53:09.007 回答