Reload association/related collection in NHibernate

偶尔善良 提交于 2019-12-03 21:39:33

NHibernate has a built in method to do exactly what I think you are asking. (ISession.CreateFilter)

For example if you have a Customer entity loaded named customer which has a collection of Orders named Orders, you can load the orders by doing this.

var orderQuery = session.CreateFilter(customer.Orders, string.Empty);  
var orders = orderQuery.List<Order>();

This is equivilant to the following, just a little cleaner.

var orderQuery = session.CreateQuery("from orders o where o.Customer.id = :customerId")
                        .SetParameter("customerId", customer.Id);
var orders = orderQuery.List<Order>();

If you want to filter the collection, an hql where clause can be passed as the second argument to ISession.CreateFilter(object, string)

Why not just load the Order and access its Details collection? If you were able to just load the collection, you would not be able to add to the collection because an Order is required for the relationship.

I think you are misusing NHibernateUtil.Initialize. Its purpose is to force initialization of a proxy collection (lazy loaded) in special cases. Eager loading is the opposite of lazy-loading; in that scenario the collection would always be loaded with its parent object and there would be no need for a proxy. If you already have the Order object then accessing the Details collection will cause it to be loaded. If you want eager fetch you can set that in the mapping options.

The recommended way of querying objects in NHibernate is using either the Criteria API or HQL. Do you have any reason against any of these two approaches?

var details = session.CreateCriteria<OrderDetails>().List<OrderDetails>();
var details = session.CreateQuery("from OrderDetails").List<OrderDetails>();

UPDATE:

If you want to load only the association without loading the parent object you could use the following query:

var details = session.CreateQuery(
        "select " + 
        "  orderDetail" + 
        "from " + 
        "  Order order," + 
        "  OrderDetail orderDetail " + 
        "where " + 
        "  orderDetail in elements(order.Details)"
    )
    .List<OrderDetail>();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!