我在看这个
注意不要急于同时获取多个集合属性.虽然这个声明可以正常工作:
var employees = session.Query().Fetch(e => e.Subordinates).Fetch(e => e.Orders).ToList();
我需要获取2个引用,所以我需要做类似的事情.有没有更好的方法来做到这一点.
我不能这样做,.ThenFetchMany()
因为它进入了儿童对象,但我在同一级别上的那些.
Flo*_*Lim 32
那么,查询仍然会返回你想要的结果,但表示,它会返回一个笛卡尔乘积,即SQL查询将返回count(e.Subordinates) * count(e.Orders)
结果,这可能相当迅速增加,特别是如果你已经不仅仅是两个集合更多.
NHibernate 在2.1版本中引入了Futures.不幸的是,目前的NHibernate 3.0版本似乎没有办法让它们与NHibernate.Linq(session.Query<T>()
)一起使用.
Futures允许您在一次往返数据库中执行多个查询(只要数据库支持它,但大多数都支持它).在这种情况下,您将只有count(e.Subordinates) + count(e.Orders)
结果,这显然是最小的.
期货使用标准API,HQL,它们应该与新的QueryOver API一起使用(我还没有测试过).
NHibernate.Linq确实有Query().ToFuture()和Query().ToFutureValue(),但到目前为止我只在使用它时才得到Exceptions.
编辑:
我刚刚再次检查了Linq API,如果你不使用Fetch,它似乎正在工作.以下将导致在一次往返中执行的三个SQL查询.返回的总行数将是1 + count(下属)+ count(Orders).
int id = 1;
// get the Employee with the id defined above
var employee = repo.Session.Query<Employee>()
.Where(o => o.Id == id)
.ToFuture<Employee>();
// get the Subordinates (these are other employees?)
var subordinates = repo.Session.Query<Employee>()
.Where(o => o.HeadEmployee.Id == id)
.ToFuture<Employee>();
// get the Orders for the employee
var orders = repo.Session.Query<Order>()
.Where(o => o.Employee.Id == id)
.ToFuture<Order>();
// execute all three queries in one roundtrip
var list = employee.ToList();
// get the first (and only) Employee in the list, NHibernate will have populated the Subordinates and Orders
Employee empl = list.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
感谢您将此标记为答案.
归档时间: |
|
查看次数: |
9264 次 |
最近记录: |