NHibernate Eager Loading with Queryover API on a complex object graph

蓝咒 提交于 2019-12-22 05:09:09

问题


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?


回答1:


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.




回答2:


You have to load all your entities in one QueryOver clause to get rid of proxies. But in this case you will have a lot of joins in your query, so I recommend to use lazy loading with batching.



来源:https://stackoverflow.com/questions/6077741/nhibernate-eager-loading-with-queryover-api-on-a-complex-object-graph

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