Brian Rosamilia
Brian Rosamilia

Reputation: 1486

Fluent Nhibernate loading not falling back on lazy loading? (grandchild entities)

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!

Upvotes: 0

Views: 252

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

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.

Upvotes: 1

Related Questions