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.
There are two problems:
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 usesjoin
rather thansubselect
which is what I really want it to do. Usingjoin
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.As a result of problem 1 - duplication of objects nested more than one level deep in some cases.
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.
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.
精彩评论