the problem is you are trying to load everything in a single call. rather, load the root with it's children, then lazy load the grand children. depending on how many children/grand children there are you can use use batched lazy loading. (fetch=select batch-size=#).
another thing to consider when eager loading large graphs is collection types. if the collection is a set then NH will manage removing duplicates, otherwise there is the possibility of the collection containing multiple instances of the same entity. This is by design as the different collection implementations have different behavior. I usually try to use Get/Load/FutureValue as much as possible. during preformance tuning I can then convert to criteria if necessary. A single query that returns everything is not always the best solution to retrieving data. there are times when multiple queries return smaller data sets will preform better. On Mar 14, 11:43 pm, Jopo <[email protected]> wrote: > Hi Everyone, > > I have a team currently using NHibernate on a new project. Everyone is > getting familiar with criteria queries and retreiving objects is > almost as simple as writing SQL. > > The main problem we are having, is eager loading complex and large > aggregates in one query, where going down three levels or more would > return duplicate results (see NHibernate in Action pg 225). Ideally it > would be great if all our aggregates are broken down to smaller > aggregates of only a few objects, but on rare ocassions our aggregates > are complex going down three levels or more. I understand we could > just set the mappings to "lazy = false" and Join="outer" for objects > within the aggregate and everything would be loaded nice and > efficiently in one query. But in some cases we don't want to see all > the data of an aggregate. > > Say if I have Class A which is the aggregate root of Classes B, C. And > Class A has a collection of Class B and Class B has a collection of > Class C. If I eager load classes A, B and C in a single query using > criteria, the collection of Class B in Class A is duplicated. The > first and last Classes in the object graph are fine (in this case > Class A and Class C). > > Here is an example of my query: > > var objectAList = UnitOfWork.CurrentSession.CreateCriteria<ObjectA>() > .CreateAlias("ObjectBList", "objectB", JoinType.LeftOuterJoin) > .SetFetchMode("objectB", FetchMode.Eager) > .CreateAlias("objectB.ObjectCList", "objectC", JoinType.LeftOuterJoin) > .SetFetchMode("objectc", FetchMode.Eager) > .Add(Restrictions.Eq("Id", objectAId)) > .SetResultTransformer(Transformers.DistinctRootEntity) > .Future<ObjectA>(); > ObjectA objectA = objectAList != null ? objectAList.FirstOrDefault() : > new ObjectA(); > > I use a separate method to remove duplicates from the list. I am using > NHibernate 2.1.2 and the collection mappings are set to "lazy = true". > My collections are of type IList. > > Is there a clean way to remove duplicates or not include any duplicate > results in the query? > > Any help or input would be very much appreciated. > > Thanks, > Joe -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
