Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Eager Loading with Queryover API on a complex object graph

I've got a pretty complex object graph that I want to load in one fell swoop.

Samples have Daylogs which have Daylog Tests which have Daylog Results

Daylog Tests have Testkeys, Daylog Results have Resultkeys, and TestKeys have Resultkeys.

I'm using the QueryOver API and Future to run these all as one query, and all the data that NHibernate should need to instantiate the entire graph IS being returned, verfied 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 end up with proxies to represent the relationship between Testkey and ResultKey, even though I'm specifically loading that relationship.

I think this entire query is probably representative of a poor understanding of the QueryOver API, so I would like any and all advice on it, but primarily, I'd like to understand why I get a proxy and not a list of results when later I try to get daylogresult.resultkey.testkey.results.

Any help?

like image 430
tom.dietrich Avatar asked May 20 '11 21:05

tom.dietrich


1 Answers

The answer was to call NHibernateUtil.Initialize on the various objects. Simply pulling the data down does not mean that NHibernate will hydrate all the proxies.

like image 134
tom.dietrich Avatar answered Nov 17 '22 22:11

tom.dietrich