NHibernate Eager loading multi-level child objects

人走茶凉 提交于 2019-12-18 10:23:09

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/563250/nhibernate-eager-loading-multi-level-child-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!