O'Reilly logo

NHibernate 3.0 Cookbook by Jason Dentler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Eager loading child collections

Often, when we query for some set of entities, we also need to load some children of those entities. In this recipe, I'll show you how we can use NHibernate's Futures with the session cache to eager load the child collections of our query results.

How to do it...

  1. Complete the setup steps in the introduction at the beginning of this chapter.
  2. Add the following method to the Queries class:
    public IEnumerable<Product> GetAllProducts()
    {
      var products = _session.CreateQuery(
        @"from Product p 
          order by p.UnitPrice asc")
        .Future<Product>();
    
      _session.CreateQuery(
        @"from Movie m
          left join fetch m.Actors")
        .Future<Movie>();
    
      return products.ToList();
    }
  3. In Program.cs, use the following code in the RunQueries method:
    static void RunQueries(ISession ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required