NHibernate Eager加载多级子对象 [英] NHibernate Eager loading multi-level child objects
问题描述
我有一个对象的层次结构,Order,Contact,Address:
public class Order {
public virtual联系BillingContact {get;组; }
}
public class Contact {
public virtual Address Address {get;组; }
}
我想通过id查询一个订单,并且急切地加载billingcontact, (
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $) ,FetchMode.Eager)
这个标准急于加载BillingContact,但是可以理解的不是BillingContact的地址。如果我添加:
.SetFetchMode(BillingContact.Address,FetchMode.Eager)
$ c $
另外请注意,这些关系是单向的:
$ b / p>
public OrderMap()
{
References(x => x.BillingContact)
.Not.Nullable()
.Cascade.All();
$ b public ContactMap()
{
HasOne(x => x.Address)
.Cascade.All()
.FetchType.Join();
$ b $ public AddressMap()
{
Map(x => x.Address1);
$ b 如何构造一个将加载子对象的标准对象?这些关系映射似乎是正确的吗?我相信你可能需要添加一个别名BillingContact允许你访问它的地址。
类似于:
var criteria = DetachedCriteria.For< Order>()
.CreateAlias(BillingContact,bc)
.SetFetchMode(BillingContact,FetchMode.Eager)
.SetFetchMode(bc.Address,FetchMode.Eager )
I have a hierarchy of objects, Order, Contact, Address:
public class Order {
public virtual Contact BillingContact { get; set; }
}
public class Contact {
public virtual Address Address { get; set; }
}
I want to query an order by id, and eager load the billingcontact, along with it's address.
var criteria = DetachedCriteria.For<Order>()
.SetFetchMode("BillingContact", FetchMode.Eager)
This criteria eager loads the BillingContact, but understandably not the address of the BillingContact. If i add:
.SetFetchMode("BillingContact.Address", FetchMode.Eager)
This does nothing to help.
Also note that these relationships are unidirectional:
public OrderMap()
{
References(x => x.BillingContact)
.Not.Nullable()
.Cascade.All();
}
public ContactMap()
{
HasOne(x => x.Address)
.Cascade.All()
.FetchType.Join();
}
public AddressMap()
{
Map(x => x.Address1);
}
How can I construct a criteria object that will load the child of the child? Do these relationship mappings seem correct?
解决方案 I believe you might need to add an alias to BillingContact to allow you access to it's Address.
Something like:
var criteria = DetachedCriteria.For<Order>()
.CreateAlias("BillingContact", "bc")
.SetFetchMode("BillingContact", FetchMode.Eager)
.SetFetchMode("bc.Address", FetchMode.Eager)
这篇关于NHibernate Eager加载多级子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!