Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy Loading in NHibernate

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.

like image 730
Ted Smith Avatar asked Dec 05 '22 06:12

Ted Smith


2 Answers

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)
like image 140
lomaxx Avatar answered Dec 08 '22 04:12

lomaxx


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" >
like image 29
Chris Avatar answered Dec 08 '22 04:12

Chris