How to Eager Load Associations without duplication in NHibernate?

Fetching Collections is a difficult operation. It has many side effects (as you realized, when there are fetched more collections). But even with fetching one collection, we are loading many duplicated rows.

In general, for collections loading, I would suggest to use the batch processing. This will execute more SQL queries... but not so much, and what is more important, you can do paging on the root list ARNomination.

See: 19.1.5. Using batch fetching you can find more details.

You have to mark your collections and/or entities with an attribute batch-szie="25".

xml:

<bag name="Contacts" ... batch-size="25">
...

fluent:

HasMany(x => x.Contacts)
  ...
  .BatchSize(25)

Please, check few arguments here:

  • NHibernate QueryOver with Fetch resulting multiple sql queries and db hits
  • Is this the right way to eager load child collections in NHibernate
  • https://stackoverflow.com/q/18419988/1679310

I concur with @RadimKöhler as soon as you eager load more than one collection then a Cartesian product always occurs. For selecting a suitable batch size then I would probably choose this to be the same as the page size as it just feels right... (no evidence why though)

There is another technique that you may feel is a better fit and that is to read this blog post by Ayende which shows you how you can send two future queries at the same time to eager load multiple collections that soul job is to load each collection singly.

However whichever route you take I suggest throwing a profiler at the results to see which performs better for you...