Eager loading with Criteria
In this recipe, we'll show you how to use CriteriaQueries to eager load the child collections of our query results.
Getting ready
Complete the Getting Ready instructions at the beginning of this chapter.
How to do it…
Create a new folder named
EagerLoadingWithCriteriain the project.Add a new
classnamedRecipeto the folder:using NH4CookbookHelpers.Queries; using NH4CookbookHelpers.Queries.Model; using NHibernate; using NHibernate.Transform; namespace QueryRecipes.EagerLoadingWithCriteria { public class Recipe : QueryRecipe { protected override void Run(ISession session) { var book = session.CreateCriteria<Book>() .SetFetchMode("Publisher", FetchMode.Join) .UniqueResult<Book>(); Show("Book:", book); var movies = session.CreateCriteria<Movie>() .SetFetchMode("Actors", FetchMode.Join) .SetResultTransformer( ...