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:
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 first loads the list of all person and then when we access the hobbies of each person, it lazy loads its respective list of hobbies. In many circumstances, this might be the desired behavior, specifically if we know that we only want to access properties of the person entity but not its hobbies, or if we only want to access...