0

When querying nhibernate, I'm seeing some odd behavior

When I write a query like this

Repository.QueryOver<Entity>()
    .Fetch(x => x.Child1).Eager
    .Fetch(x => x.child2).Eager

It will eagerly grab child1 and child2 entities, but there are grandchildren for child1 and child2 that aren't lazily loaded. I'm a bit confused on how to achieve this.

In my nhibernate mappings, it seems to have no affect on the laziness or eagerness of grandchildren and I require at least some entities be eagerly loaded to avoid the N+1 query problem.

I'm also wondering how I could eagerly load grandchildren entities under my original entity.

Any help or ideas are appreciated!

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Brian Rosamilia
  • 1,448
  • 14
  • 24
  • The way to go is [batch fetching](http://stackoverflow.com/a/20970816/1679310) and here it is in [fluent](http://stackoverflow.com/a/20999028/1679310) – Radim Köhler Dec 07 '15 at 16:59
  • At what level(s) in my mapping do I need to declare batching? Then does anything need to be done in the query API to make sure it uses the batches? Sorry, Nhibernate can be really unclear about this in documentation – Brian Rosamilia Dec 07 '15 at 17:13
  • I put all the information into answer. hope it will help a bit – Radim Köhler Dec 07 '15 at 17:37

1 Answers1

1

I would suggest to use the batch-fetching. As discussed here, the fluent syntax is:

1) the collection setting

HasMany<MyEntity>(x => x.Entities)
  .BatchSize(100);

2) the class level setting

public MyEntityMap()
{
    Id(x => x....
    ...
    BatchSize(100);

This setting should be applied on every collection and every class. To do that with fluent - we can use Conventions - see more e.g. here

'IClassConvention' - Use to alter your ClassMaps values. You can't change properties, collections, etc with this convention, only alter the settings such as Lazy and BatchSize.

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335