NHibernate Eager 加载多级子对象 [英] NHibernate Eager loading multi-level child objects

查看:17
本文介绍了NHibernate 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; }
}

我想通过 id 查询订单,并预先加载 billingcontact 及其地址.

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)

这个标准急切加载 BillingContact,但可以理解不是 BillingContact 的地址.如果我添加:

This criteria eager loads the BillingContact, but understandably not the address of the BillingContact. If i add:

     .SetFetchMode("BillingContact.Address", FetchMode.Eager)

这没有任何帮助.

另请注意,这些关系是单向的:

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?

推荐答案

我相信您可能需要为 BillingContact 添加别名以允许您访问它的地址.

I believe you might need to add an alias to BillingContact to allow you access to it's Address.

类似于:

var criteria = DetachedCriteria.For<Order>()
  .CreateAlias("BillingContact", "bc")
  .SetFetchMode("BillingContact", FetchMode.Eager)
  .SetFetchMode("bc.Address", FetchMode.Eager)

这篇关于NHibernate Eager 加载多级子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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