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

查看:78
本文介绍了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对象(使用Criteria)来实现此目的,并且在每个对象中,我会执行SetFetchMode("...", FetchMode.Eager)-请参见

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在同一往返中执行多个选择查询-一次是针对从根到叶子的每个路径(A.B.C.D),这很好,但是使用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.

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

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.

推荐答案

您只需创建一个调用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天全站免登陆