Fetch vs FetchMany in NHibernate Linq provider

Simon picture Simon · Dec 9, 2010 · Viewed 25.7k times · Source

NHibernate eager loading can be done using Fetch and FetchMany, as described in NHibernate Linq Eager Fetching on Mike Hadlow's blog.

What is the difference between these two methods and under what circumstance would each be used?

Answer

Diego Mijelshon picture Diego Mijelshon · Dec 9, 2010

Fetch should be used for references and FetchMany for collections.

This is particularly important because only FetchMany can be combined with ThenFetchMany to fetch "grandchildren" collections.

Example:

session.Query<User>()
       .FetchMany(u => u.Orders)
       .ThenFetchMany(o => o.OrderItems)