My question is very similar to this one (did not answer it): Nhibernate: excellent results in the second level collection
I have this object model:
class EntityA
{
...
IList<EntityB> BList { get; protected set; }
...
}
class EntityB
{
... does NOT reference its parent EntityA...
IList<EntityC> CList { get; protected set; }
}
This is a one-to-many relationship. EntityB and C do not have an object reference for their parent object.
I would like to fully load the collections by doing something like the following three SQL queries to avoid a Cartesian join:
SELECT id, ... FROM EntityA;
SELECT id, idA, ... FROM EntityB;
SELECT id, idB, ... FROM EntityC;
At the same time, DAL has all the information to properly fill in objects. But since EntityB does not know who its parent is, it has which should be nHibernate, which takes care of filling collections properly.
Can this be done?
, , , DAL .
ICriteria criteria = session.CreateCriteria<EntityA>()
.SetFetchMode("BList", FetchMode.Join)
.SetFetchMode("BList.CList", FetchMode.Join)
.SetResultTransformer(new DistinctRootEntityResultTransformer());
IList<EntityA> listA = criteria.List<EntityA>();
foreach (EntityA objA in listA) {
objA.BList = objA.BList.Distinct().ToList();
foreach (EntityB objB in objB.BList) {
objB.CList = objB.CList.Distinct().ToList();
}
}