在 linq 中多次提取以进行休眠 [英] Multiple Fetches in linq to nhibernate

查看:13
本文介绍了在 linq 中多次提取以进行休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看这个

注意不要急切地去取多个集合属性在同时.虽然这个说法会正常工作:

Be careful not to eagerly fetch multiple collection properties at the same time. Although this statement will work fine:

var 员工 =session.Query().Fetch(e => e.Subordinates).Fetch(e => e.Orders).ToList();

var employees = session.Query() .Fetch(e => e.Subordinates) .Fetch(e => e.Orders).ToList();

我需要获取 2 个引用,所以我需要做类似的事情.有没有更好的方法来做到这一点.

I need to fetch 2 references so I would need to do something like that. Is there a better way of doing this.

我不能做 .ThenFetchMany() 因为它进入子对象但我在同一级别追求的那些.

I can't do .ThenFetchMany() as it goes to the into the child objects but the ones I am after on the same level.

推荐答案

好吧,查询还是会返回你想要的结果,但是如前所述,它会返回一个笛卡尔积,即 SQL 查询会返回 count(e.Subordinates) * count(e.Orders) 结果,这可能会很快加起来,特别是如果您有两个以上的集合.

Well, the query will still return the results you want, but as stated, it will return a cartesian product, i.e. the SQL query will return count(e.Subordinates) * count(e.Orders) results, which could add up pretty quickly, especially if you have more than just two collections.

NHibernate 在 2.1 版本中引入了Futures.不幸的是,在当前的 NHibernate 3.0 版本中似乎没有办法让它们与 NHibernate.Linq (session.Query()) 一起工作.

NHibernate introduced Futures with the 2.1 release. Unfortunately there seems to be no way in the current NHibernate 3.0 release to make them work with NHibernate.Linq (session.Query<T>()).

Futures 允许您在一次到数据库的往返中执行多个查询(只要数据库支持,但大多数都支持).在这种情况下,您将只有 count(e.Subordinates) + count(e.Orders) 结果,这显然是最小值.

Futures allow you to perform multiple queries in one roundtrip to the database (as long as the DB supports it, but most do). In that case you will only have count(e.Subordinates) + count(e.Orders) results, which is obviously the minimum.

Futures 使用标准 API、HQL,并且它们应该使用新的 QueryOver API(我还没有测试过).

Futures work with the criteria API, HQL and they are supposed to work with the new QueryOver API (I have not tested that, yet).

NHibernate.Linq 确实有 Query().ToFuture() 和 Query().ToFutureValue(),但到目前为止我只在使用它们时得到异常.

NHibernate.Linq does have Query().ToFuture() and Query().ToFutureValue(), but so far I only get Exceptions when I use them.

我刚刚再次检查了 Linq API,如果您不使用 Fetch,它似乎可以正常工作.以下将导致在一次往返中执行三个 SQL 查询.返回的总行数将为 1 + count(Subordinates) + count(Orders).

I just checked again for the Linq API and it seems as if it is working if you do not use Fetch. The following will result in three SQL queries that are executed in one roundtrip. The total number of rows return will be 1 + count(Subordinates) + 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();

感谢您将其标记为答案.

Thank you for having marked this as the answer anyway.

这篇关于在 linq 中多次提取以进行休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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