Eager fetching

You can disable lazy loading in mapping and that would load all collections/associations eagerly all the time. But as stated earlier, eagerly loading associations all the time is not a good idea. However, at times you do need to load a particular association eagerly. For this reason, NHibernate lets you specify, during the query, that a particular association should be loaded eagerly. Let's take a look at examples of different querying mechanisms to see how to eagerly load associations.


To eagerly load associations in HQL, you need to add keyword fetch after the join keyword, as shown next:

select e from Employee as e join fetch e.Benefits where e.Firstname = :firstName

Preceding HQL query loads all employee instances with matching ...

Get Learning NHibernate 4 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.