在NHibernate中重新加载关联/相关集合 [英] Reload association/related collection in NHibernate

查看:55
本文介绍了在NHibernate中重新加载关联/相关集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我具有带有OrderDetails列表的Order实体,则可以使用NHibernateUtil.Initialize(Order.Details)轻松地渴望将详细信息与订单一起加载.因此,显然NHibernate具有生成sql语句的所有信息. 但是如何在不手动创建条件的情况下仅在数据库中查询Details(类似于Entity Framework中的CreateSourceQuery)? 是否有类似NHibernateUtil.GetList(Order.Details)的东西?

If I have Order entity with a list of OrderDetails I can easily eager load the detail along with the order by using NHibernateUtil.Initialize(Order.Details). So obviously the NHibernate have all the information to generate the sql statement. But how do I query the database for just the Details (similar to CreateSourceQuery in Entity Framework) without manually creating a criteria? Is there something like NHibernateUtil.GetList(Order.Details)?

更新: 使用达林的答案,我最终得出了这一结论.这足够通用了,我可以在实体基类中实现它

Update: Using Darin's answer this what I finally ended up with. This is generic enough i can implement it in entity base class

Dim entity as EntityBase
Dim queryString = String.Format("select entityAlias.{1} from {0} entityAlias where entityAlias.id = :ID", entity.GetType.Name, collectionPropertyName)
Dim query = Session.CreateQuery(queryString).SetParameter("ID", entity.ID)
Return query.List

推荐答案

NHibernate具有一个内置方法,可以完全按照我的要求进行操作. (ISession.CreateFilter)

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

例如,如果您加载了一个名为customer的Customer实体,该实体具有一个名为Orders的Orders集合,则可以通过执行此操作来加载订单.

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>();

如果要过滤集合,可以将hql where子句作为第二个参数传递给ISession.CreateFilter(object, string)

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

这篇关于在NHibernate中重新加载关联/相关集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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