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…

  1. Create a new folder named EagerLoadingWithCriteria in the project.
  2. Add a new class named Recipe to 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>() ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.