NHibernate的未来对象图许多查询 [英] NHibernate Future Object Graph Many Queries

查看:161
本文介绍了NHibernate的未来对象图许多查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var Dads = db.Session.Query<Parent>().Where(P => P.EntityKey == Id)
             .ToFuture<Parent>();
var Kids = db.Session.Query<Kid>().Where(K => K.Parent.EntityKey == Id)
             .ToFuture<Kid>();

当我打电话VAR爸爸= dads.ToList()我看到了一批跨走线并显示探查。

when I call var Dad = dads.ToList() I see the batch go across the wire and show in profiler.

问题是枚举它仍然是发送一次性查询到数据库

Problem is when enumerating the collection it is still sending one off queries to the db

例如。

for each (Kid kid in Dad.Kids) // This seems to hit the database 
{
   Teach(kid);
}

发送SQL查询和访问数据库来获得每个孩子。为什么对象图不填充?或者这是预期的行为?

Sends a SQL query and hits the database to get each kid. Why is the object graph not populated? or is this expected behavior?

推荐答案

这行为是可以预期的。您只需告诉NHibernate的从批次中的数据库,告诉它做得到两个集合。但是,你不告诉它,它们是相关的。与期货NH查询不要把实体一道执行它们后,除非他们被告知有一个联接这样做。

That behaviour is to be expected. You are simply telling NHibernate to get two collections from the database in a batch, which it is doing as told. However, you are not telling it that they are related. NH Queries with Futures do not put entities together after executing them unless they are told to do so with a join.

如果您不执行期货单独的查询,你不会指望父实体突然将孩子集中了。基本上,期货允许你在一个往返运行的东西。如果查询正好有几个子集合公共根(例如,以避免笛卡尔积),那么NH能结合了多个集合成一个实体。

If you executed the separate queries without Futures you would not expect the Parent entity to suddenly have the child collection filled. Basically, Futures allow you to run things in one roundtrip. If the queries happen to have a common root with several child collections (e.g. to avoid a cartesian product), then NH is able to "combine" several collections into one entity.

不幸的是与NH LINQ API和 ToFuture()方法加入似乎不会造成当前问题(NH 3.0或3.1)实现。可能需要使用QueryOver阿比在这种情况下

Unfortunately joins with the NH LINQ Api and the ToFuture() method seem to pose a problem in the current (NH 3.0 or 3.1) implementation. You may need to use the QueryOver Api in that case.

在一个侧面说明,我认为方法名是不恰当的。

On a side note, I think the method name is not appropriate.

编辑:后编辑问题的方法名称现在是确定

After Edit of the question the method name is now ok.

这篇关于NHibernate的未来对象图许多查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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