简体   繁体   English

NHibernate:如何在数据库的单次往返中执行许多子孙(对象图)的热切子选择?

[英]NHibernate: How to perform eager subselect fetching of many children & grandchildren (object graph) in a single round-trip to the database?

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. 首先,请不要试图争论我做出的热切负载 - 遍历对象图并导致(通过延迟加载)甚至超过一次往返数据库只是不是一种选择。

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 . 我想获取根对象,以及它的子,孙子,曾孙等的子集 。目前我通过创建多个Future对象(使用Criteria)并在每个对象中执行此操作,我执行SetFetchMode("...", FetchMode.Eager) - 请参阅Ayende的帖子Sam的第3条评论 There are two problems: 有两个问题:

  1. NHibernate performs multiple select queries in the same round-trip - one for each path from root to a leaf ( ABCD ), which is great, but uses join rather than subselect which is what I really want it to do. NHibernate在同一个往返中执行多个选择查询 - 从根到叶子( ABCD )的每个路径一个,这很好,但使用join而不是subselect ,这是我真正想要它做的。 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. 使用join意味着需要从数据库发送大量数据,需要进行解析,而nhibernate需要做更多的工作而不是必要的。

  2. As a result of problem 1 - duplication of objects nested more than one level deep in some cases. 问题1的结果 - 在某些情况下,对象的重复嵌套在一个以上的深度。

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 . 我通过将我的集合设置为Set来“解决”第二个问题,但后来我失去了排序能力 - 因为我必须指定ISet作为接口,我的代码无法知道该集合是否真的是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. 我已经在网上搜索了答案,显然我不是第一个打到这堵墙的人。

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). 您可以创建一个单独的查询,只需调用一次SetFetchMode,并使用MultiCriteria(或Futures或您想要使用的任何内容)一次性运行它们。 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. 这将在单次往返中为您提供单一结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM