Nhibernate 一对多延迟加载未按预期工作
考虑以下场景:
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 。
映射如下: A 映射...
<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" />
有什么想法吗? 谢谢。
Consider the following scenario:
Class A has a one-to-many relationship to Class B.
Class B has a many-to-one relationship to Class C.
class A {
IList<B> BList {get;set;}
}
class B {
C CMember{get;set;}
}
class C {
//null
}
If I load class B from the database using something like
IList<B> result = query.List<B>();
everything works as expected,
However if I do something like:
DetachedCriteria query = DetachedCriteria.For(typeof(A));
query.CreateAlias("B", "B", JoinType.InnerJoin);
IList<A> result = query.List<A>();
then, NHibernate will also select from table C and load all Cs.
Mappings below:
A mapping...
<bag name="BList " table="B" lazy="true" inverse="false">
<key column="id" />
<one-to-many class="B" />
</bag>
B mapping...
<many-to-one class="C" name="CMember" column="idC" lazy="proxy" outer-join="true" />
Any Ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我创建了一个示例应用程序来测试您概述的场景,但我无法重现 Criteria 和 DetachedCriteria 之间的差异,它们都为我返回了相同的结果。
加载是否遵循惰性属性取决于外连接属性,当设置它时,我总是急切地加载C,当我删除外连接属性时,它通过代理延迟加载C。
因此一种可能的解决方案是将 B 映射更改为:
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: