NHibernate:如何执行许多孩子的急切子选择和获取孙子(对象图)单次往返数据库? [英] NHibernate: How to perform eager subselect fetching of many children & grandchildren (object graph) in a single round-trip to the database?

查看:30
本文介绍了NHibernate:如何执行许多孩子的急切子选择和获取孙子(对象图)单次往返数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,请不要试图争辩我不要进行急切加载 - 遍历对象图并导致(通过延迟加载)甚至不止一次往返数据库是不可行的.

First, please don't try to argue me out of doing the eager load - traversing the object graph and causing (by lazy loading) even more than ONE round-trip to the database is just not an option.

我有一个大对象图.我想获取根对象,以及它的子、孙、曾孙等的 子集.目前我通过创建多个 Future 对象(使用标准)来做到这一点在每一个中,我都会执行 SetFetchMode("...", FetchMode.Eager) - 请参阅 Ayende 的帖子Sam 的第三条评论.有两个问题:

I have a big object graph. I want to fetch the root object, plus a subset of its children, grandchildren, great-grandchildren, etc. Currently I do this by creating multiple Future objects (with Criteria) and in each one, I do SetFetchMode("...", FetchMode.Eager) - see Ayende's post and Sam's 3rd comment here. There are two problems:

  1. NHibernate 在同一次往返中执行多个选择查询 - 从根到叶 (ABCD) 的每个路径一个,这很好,但使用 join 而不是 subselect 这是我真正想要做的.使用 join 意味着需要从数据库发送大量数据,需要进行解析,并且 nhibernate 需要做的工作比必要的要多.

  1. NHibernate performs multiple select queries in the same round-trip - one for each path from root to a leaf (A.B.C.D), which is great, but uses join rather than subselect which is what I really want it to do. Using join means a ton of data needs to be sent from the database, needs to be parsed, and nhibernate needs to do a lot more work than necessary.

由于问题 1 - 在某些情况下,对象重复嵌套超过一层.

As a result of problem 1 - duplication of objects nested more than one level deep in some cases.

我通过将集合设置为 Set 来解决"第二个问题,但随后我失去了排序能力 - 因为我必须将 ISet 指定为接口,所以我的代码无法知道如果集合真的是 OrderedSet.

The second problem I "solved" by setting my collections to be Set, but then I lose the ordering ability - since I must specify ISet as the interface, there's no way for my code to know if the set is really an OrderedSet.

有谁知道如何在一次往返中急切加载一个对象以及几个深度嵌套的集合,但不使用连接?

Does anyone know how to perform, in a single round-trip, eager loading of an object plus several deeply nested collections, but not using join?

我将非常感激!我在网上搜索了答案,显然我不是第一个碰壁的人.

I'd be extremely grateful! I've scoured the web for answers, apparently I'm not the first to hit this wall.

推荐答案

您可以创建一个单独的查询,只需 1 次调用 SetFetchMode 并使用 MultiCriteria(或 Futures 或您想使用的任何东西)一次性运行它们.之后,只有第一个查询的结果与您相关.这将在一次往返中为您提供一个结果.

You can create a separate queries with only 1 call to SetFetchMode and run them in one go using MultiCriteria (or Futures or whatever you want to use). After that, only the result from the first query is relevant to you. This will give you a single result in an single round-trip.

这篇关于NHibernate:如何执行许多孩子的急切子选择和获取孙子(对象图)单次往返数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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