views:

52

answers:

2

When must we use eager loading in NHibernate? What is it's usage?

+1  A: 

One usage is when you will cache or store an object graph (in ASP.NET Cache for instance). If you don't store the whole graph, you would be missing information on a detached object. You can reattach objects of course, but that would probably be a new roundtrip to the database anyway.

If you don't eager load your collections, you would need to touch every one of the to invoke the lazy fetch. In those cases, an eager fetch is much more useful.

jishi
Thank you jishi, How can I implement eager loading in my application?
masoud ramezani
Eager loading is either defined globally on your collection (meaning, lazy="false") or at query time with ICriteria (FetchMode.Eager)
jishi
please see this : http://stackoverflow.com/questions/2987697/is-there-any-utility-or-built-in-class-in-nhibernate-that-force-a-class-to-load-iis your answer my solution?
masoud ramezani
The answer you got there is the same as I gave you. You can also use HQL to specify fetching. It is however not possible to utilize Batch-mode fetching without specifying it in your mapping, then you must depend on the first level cache and fetch data subsequently, please look up MultiCriteria/MultiQuery for that.
jishi
+1  A: 

Maybe this presentation by Udi can help you decide.

http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan

epitka