Richard
Richard

Reputation: 1739

Why is everything being eagerly loaded?

I'm trying to get to grips with QueryOver, and I was expecting this to return me a Summary item with its ReportRows collection eagerly loaded.

Update The first block of code wasn't in my original question but it was the cause of my problem - thanks to dotjoe for the answer

    // some code to create a Summary and a whole graph of child records
    ...
    // then...
    session.Save(summary);
    session.Flush(); // write the changes
    session.Evict(summary); // clear out session to check my query below is 'fresh'

    // Originally-posted code
    Summary summaryAlias = null;
    Report reportAlias = null;
    var summaryQuery =
        session.QueryOver<Summary>(() => summaryAlias)
            .Fetch(x => summaryAlias.Reports).Eager
            .Left.JoinAlias(() => summaryAlias.Reports, () => reportAlias)
            .Where(() => summaryAlias.Id == workItemId);                    

    Summary summary = summaryQuery.SingleOrDefault<Summary>();
    session.Close();

However when I hit a breakpoint after session.Close() has been called (to prevent any further lazy loading), I find that everything in my Summary class has been populated, not just the ReportRows collection.

Some examples of things that have been populated even though I wasn't expecting them to be:

ReportRow.Student
ReportRow.Programme
ReportRow.Programme.Modules (a collection)
ReportRow.Programme.Modules.Components (another collection inside each 'Module')

I'm using Fluent automappings, and I've configured it to be lazy-loaded just to be sure using:

.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Always())

and also tried...

.Conventions.Add(FluentNHibernate.Conventions.Helpers.LazyLoad.Always())

Why is it loading everything?

Thanks

Upvotes: 1

Views: 136

Answers (1)

dotjoe
dotjoe

Reputation: 26940

Could you post the generated hbm.xml? example

Also, try this query and see what happens if you access any lazy properties after the using statements...

Summary summary  = null;

using(var session = factory.OpenSession())
using(var tx = session.BeginTransaction())
{
    summary = session.QueryOver<Summary>()
        //.Fetch(x => x.Reports).Eager
        .Where(x => x.Id == workItemId)
        .SingleOrDefault<Summary>();

    tx.Commit();
}

Upvotes: 1

Related Questions