NHibernate Future Object Graph Many Queries

我与影子孤独终老i 提交于 2019-12-05 23:21:25

问题


Given a multi level object graph being called using Future as:

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

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

Eg.

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

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


回答1:


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.

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.

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.

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



来源:https://stackoverflow.com/questions/5435105/nhibernate-future-object-graph-many-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!