Avoiding eager fetching

While eager fetching of collections looks like a simple and quick solution to select N+1 problem, it does introduce an issue. Since eager fetching uses left outer joins, it results in Cartesian product of matching row in root table and collection table. Following unit test illustrates this behavior. We have three employees stored in-memory database. All three employees live in London. Two of the three employees are members of a community and the third employee is member of two communities. The query in the following unit test loads all employees living in London and also eagerly fetches their Communities collection. I expect to get three employee instance back.

[Test] public void QueryOver() { using (var transaction = Database.Session.BeginTransaction()) ...

Get Learning NHibernate 4 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.