O'Reilly logo

NHibernate 3 Beginner's Guide by Gabriel N. Schenker

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 versus lazy loading

Let's assume we have the following simple domain with a Person entity that has a set of Hobby entities, as shown in the following screenshot:

Eager loading versus lazy loading

If we have stored, for example, three person entities in the database and we execute the following code snippet:

var listOfPersons = session.Query<Person>();
foreach (var person in listOfPersons)
{
  Console.WriteLine("{0} {1}", person.LastName, person.FirstName);
  foreach (var hobby in person.Hobbies)
  {
    Console.WriteLine("  {0}", hobby.Name);
  }
}

Then the result in the NHibernate profiler will look similar to the following screenshot:

This is a typical select (n+1) problem. NHibernate ...

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