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.

Reply via email to