Thanks to this article:

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/06/eager-loading-aggregate-with-many-child-collections.aspx

I found a solution by using multi-queries. Here it is:

      var sql1 = "from CustomerOrderSet b inner join fetch
b.CustomerOrders where b.Id=:id";

      var sql2 = "from CustomerOrder b inner join fetch b.OrderDetails
where b.CustomerOrderSet.Id=:id";

      var sql3 = "from OrderDetail b inner join fetch
b.ProductDetailCellUnits c inner join fetch c.Product where
b.CustomerOrder.CustomerOrderSet.Id=:id";

      var result = Session.CreateMultiQuery()

                      .Add(Session.CreateQuery(sql1))

                      .Add(Session.CreateQuery(sql2))

                      .Add(Session.CreateQuery(sql3))

                      .SetParameter("id", 39)

                      .List();

      IList list = (IList)result[0];

      CustomerOrderSet set = list[0] as CustomerOrderSet;

 It also works with multi-criteria, and it probably works with
futures.





On Aug 7, 11:04 am, abutnaru <[email protected]> wrote:
> Hi,
>
> I have this structure of entities:
>
> CustomerOrderSet
>
>         -> contains CustomerOrders (a Set can contain more than one Order)
>                 this is the entity property:
>                         public virtual IList<CustomerOrder> CustomerOrders
>                 this is the mapping:
>                         <bag name="CustomerOrders" inverse="true" 
> cascade="save-update" >
>                                 <key column="CustomerOrderSetId" />
>                                 <one-to-many class="CustomerOrder" />
>                         </bag>
>
>                 -> contains OrderDetails  (an Order can contain more than one
> Detail)
>                         this is the entity property:
>                                 public virtual IEnumerable<OrderDetail> 
> OrderDetails
>                         this is the mapping:
>                                 <bag name="OrderDetails" 
> cascade="all-delete-orphan"
> inverse="true" access="field.camelcase-underscore">
>                                         <key column="CustomerOrderId" />
>                                         <one-to-many class="OrderDetail" />
>                                 </bag>
>
>                         -> contains CellUnits (many-to-many relation: an 
> OrderDetail can
> contain more than one CellUnit, a CellUnit can be contained by more
> than one OrderDetail)
>                                 this is the entity property:
>                                         public virtual 
> IEnumerable<ProductDetailCellUnit>
> ProductDetailCellUnits
>                                 this is the mapping:
>                                         <bag name="ProductDetailCellUnits"
> table="OrderDetailXProductDetailCellUnit" access="field.camelcase-
> underscore">
>                                                 <key column="OrderDetailId" />
>                                                 <many-to-many 
> column="ProductDetailCellUnitId"
> class="ProductDetailCellUnit" />
>                                         </bag>
>
>                                 -> contains a Product (a CellUnit has a 
> corresponding Product)
>                                         this is the entity property:
>                                                 public virtual Product 
> ProductCorrespondent { get; set; }
>                                         this is the mapping:
>                                                 <many-to-one name="Product" 
> column="ProductId" class="Product"></
> many-to-one>
>
> Considering a CustomerOrderSet which has 4 CustomerOrders, each
> CustomerOrder has 3 or 4 OrderDetails, each OrderDetail has 5 or 6
> CellUnits, and each CellUnit has its corresponding Product, what is
> the best way to load the CustomerOrderSet with all this information in
> it?
>
> Ideally, the solution:
> - uses as few select statements as possible
> - creates no duplicated data in the hierarchy
> - avoids select n+1
>
> I already saw this 
> thread:http://groups.google.com/group/nhusers/browse_thread/thread/b38ab29ae...,
> but since then new features have been added to NHibernate (like
> Futures), so I am not sure which option is the right one.
> Some code examples would be great.
> Thanks.
--~--~---------~--~----~------------~-------~--~----~
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