views:

1251

answers:

5

If a Customer has many Orders attached to them. How would you lazy load the Orders List using NHibernate.

Is it something that needs to be set up mapping file? any help or an example would be great.

+3  A: 

Heres a good article:

http://blogs.chayachronicles.com/sonofnun/archive/2007/03/30/230.aspx

From the above article:

The most common is to simply mark the class with the 'lazy="true"' attribute or place 'default-lazy="true"' in the mapping declaration:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="@core.assembly@"
 default-access="nosetter.camelcase-underscore" default-lazy="true">

Or

<class name="Cei.eMerge.Core.Domain.Contacts.Contact" table="Contact" lazy="true" >
Chris
+6  A: 

Chris' suggestion is how I'd do it, however if you want to do it at runtime you can set the Fetchmode on your criteria to be lazy like so:

criteria.SetFetchMode("Orders", FetchMode.Lazy)
lomaxx
+2  A: 

To lazy-load a particular collection of an entity, use "lazy=true" on the collection mapping. For instance:

<bag name="EmploymentHistory" cascade="all" inverse="true" lazy="true">
  <key column="PersonID" />
  <one-to-many class="MyDomain.EmploymentRecord, MyDomainAssembly" />
</bag>
John Rayner
+1  A: 

All of the answers here are correct, but if there are so many Orders you migth also want to use filters, so that you don't have to load all of them.

Customer customer = session.CreateCriteria(...)
              .SetFetchMode("Orders", FetchMode.Lazy)
              .UniqueResult<Customer>();

Ilist<Order> orders = session.CreateFilter(customer.Orders," WHERE this.OrderDate < ?")
                      .SetDateTime(...).List();
Michał Piaskowski
A: 

Do you want that the Customer entity has a property that contains all his Orders ? I think that this can be quite a large collection.
I think that, the question you'll have to ask yourself is:
How many times do I really need to have direct access to the Orders of a Customer ?

Perhaps, in this case, you don't want to have a bidirectional association ? Perhaps you don't want to have an Orders collection in your Customer class.
Then, I would add a method to my Order Repository, which has the following signature:

IList<Order> GetOrdersForCustomer( Customer c );

But, I don't know if this is feasible for your situation.

Frederik Gheysels