NHibernate Eager Loading using Queryover API on a graph of complex objects

I have a rather complex graph of objects that I want to load at one go max.

There are diaries in the samples that have daily tests that have Daylog Results

Daily log tests have Testkeys, Daylog results have Resultkeys and TestKeys have Resultkeys.

I use the QueryOver API and Future to run all of this as a single query, and all the data that NHibernate should have created for the entire IS graph is returned, confirmed by NHProf.

public static IList<Daylog> DatablockLoad(Isession sess, ICollection<int> ids) { var daylogQuery = sess.QueryOver<Daylog>() .WhereRestrictionOn(dl => dl.DaylogID).IsIn(ids.ToArray()) .Fetch(dl => dl.Tests).Eager .TransformUsing(Transformers.DistinctRootEntity) .Future<Daylog>(); sess.QueryOver<DaylogTest>() .WhereRestrictionOn(dlt => dlt.Daylog.DaylogID).IsIn(ids.ToArray()) .Fetch(dlt => dlt.Results).Eager .Inner.JoinQueryOver<TestKey>(dlt => dlt.TestKey) .Fetch(dlt => dlt.TestKey).Eager .Inner.JoinQueryOver<ResultKey>(tk => tk.Results) .Fetch(dlt => dlt.TestKey.Results).Eager .Future<DaylogTest>(); sess.QueryOver<DaylogResult>() .Inner.JoinQueryOver(dlr => dlr.DaylogTest) .WhereRestrictionOn(dlt => dlt.Daylog.DaylogID).IsIn(ids.ToArray()) .Fetch(dlr => dlr.ResultKey).Eager .Fetch(dlr => dlr.History).Eager .Future<DaylogResult>(); var daylogs = daylogQuery.ToList(); return daylogs; } 

However, I still get proxies to represent the relationship between Testkey and ResultKey, although I intentionally load this relationship.

I think this whole query probably represents a poor understanding of the QueryOver API, so I would like all the advice on this, but first of all, I would like to understand why I get a proxy server and not a list of results when later i try to get daylogresult.resultkey.testkey.results.

Any help?

+7
source share
2 answers

The answer was to call NHibernateUtil.Initialize for various objects. Simply pulling data does not mean that NHibernate will humidify all proxies.

+5
source

You must load all your entities in one QueryOver clause to get rid of the proxy. But in this case, you will have many unions in your request, so I recommend using lazy loading with batch loading.

0
source

All Articles