Eager loading with HQL
In this recipe, we'll show you how to use HQL queries to eager load the child collections of our query results.
Getting ready
Complete the Getting Ready section at the beginning of this chapter.
How to do it…
Create a new folder named
EagerLoadingWithHql
in the project.Add a new class named
Recipe
to the folder:using NH4CookbookHelpers.Queries; using NH4CookbookHelpers.Queries.Model; using NHibernate; namespace QueryRecipes.EagerLoadingWithHql { public class Recipe : QueryRecipe { protected override void Run(ISession session) { var book = session.CreateQuery(@" from Book b left join fetch b.Publisher") .UniqueResult<Book>(); Show("Book:", book); var movies = session.CreateQuery(@" from Movie m left join fetch m.Actors") .SetResultTransformer( Transformers.DistinctRootEntity) .List<Movie...