在复杂对象图上使用Queryover API进行NHibernate急切加载 [英] NHibernate Eager Loading with Queryover API on a complex object graph

查看:67
本文介绍了在复杂对象图上使用Queryover API进行NHibernate急切加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的对象图,我想在一个跌落中加载 猛扑.

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

样本具有Daylog,而Daylog具有Daylog,测试具有Daylog 结果

Samples have Daylogs which have Daylog Tests which have Daylog Results

Daylog测试具有Testkey,Daylog结果具有Resultkey,以及 TestKey具有Resultkey.

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

我正在使用QueryOver API和Future将它们全部作为一个查询运行, 以及NHibernate实例化整个实例所需的所有数据 该图形IS正在返回,并由NHProf验证.

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;
                }

但是,我仍然以代理来表示这种关系 在Testkey和ResultKey之间,即使我正在专门加载 这种关系.

However, I still end up with proxies to represent the relationship between Testkey and ResultKey, even though I'm specifically loading that relationship.

我认为整个查询很可能代表着一个穷人 了解QueryOver API,因此我希望获得所有建议 在它上面,但是首先,我想了解为什么我得到了代理而不是 结果列表,稍后我尝试获取时 daylogresult.resultkey.testkey.results.

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.

有帮助吗?

推荐答案

答案是在各种对象上调用NHibernateUtil.Initialize.简单地提取数据并不意味着NHibernate会水合所有代理.

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.

这篇关于在复杂对象图上使用Queryover API进行NHibernate急切加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆