Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Future Object Graph Many Queries

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?

like image 614
Firegarden Avatar asked Mar 25 '11 16:03

Firegarden


1 Answers

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.

like image 118
Florian Lim Avatar answered Sep 28 '22 07:09

Florian Lim