0

Im dealing with N+1 problem, and I want to eager load a collection in an especific query, but I am getting the "failed to lazily initialize a collection" error, or the query keep lazy loading.

I have tried many suggestions given by stackmembers but none worked.

My entity example:

public class Bar
{
   private _IList<Foo> _fooList;
   public IEnumerable<Foo> FooList {get{return _fooList;} }
}

The map example:

HasMany(d => d.FooList)
                .Access.CamelCaseField(Prefix.Underscore)
                .KeyColumn("IdBar")
                .Cascade.AllDeleteOrphan()
                .Inverse();

Queries that throws error (failed to lazily initialize a collection):

var test = GetSession().Query<Bar>()
                .Fetch(b => b.FooList);

IList<Bar> bars= GetSession().QueryOver<Bar>()
                .Fetch(d => d.FooList).Eager
                .List();

Query that still with N+1 problem:

        IList<Bar> bars= GetSession().QueryOver<Bar>()
            .Fetch(d => d.FooList).Eager
            .JoinQueryOver<Foo>(d => d.FooList)
            .Where(f => f.Active).List<Bar>();

        Foo fooAlias = null;
        IList<Bar> bars= GetSession().QueryOver<Bar>()
            .Fetch(d => d.FooList).Eager
            .JoinAlias(d => d.FooList, () => fooAlias)
            .List();

I need help to solve this N+1 problem, eager loading the FooList collection.

Tiago B
  • 1,937
  • 14
  • 34

2 Answers2

0

Have you used HQL along with ... LEFT JOIN FETCH ...? Did it provide same issues too?

Martin Podval
  • 1,097
  • 1
  • 7
  • 16
  • No, I dont know how to use HQL yet. I will search for it – Tiago B Sep 18 '13 at 12:22
  • It's better to use HQL according my experiences as sometimes it's hard to model some kind of queries in fluent approach. I would use HQL and solve the problem later once it would happen also in it. – Martin Podval Sep 18 '13 at 13:22
0

Mapping should use

.Access.ReadOnlyPropertyThroughCamelCaseField

instead.

MichaC
  • 13,104
  • 2
  • 44
  • 56