2

I'm trying to eagerly load an entity with nested collections. As follows:

Parent -> Children1 -> GrandChild -> GreatGrandChildren

Here is one of my 4 attempts (this was the most promising):

Parent parent = null;
            Child child = null;
            GrandChild grandChild = null;
            GreatGrandChild greatGreatChild = null;
            var result = CurrentSession.QueryOver<Parend>(() => conj)
                .Where(c => c.Id == id)
                .JoinAlias(() => parent.Children, () => child)
                .JoinAlias(() => child.GrandChild, () => grandChild)
                .JoinAlias(() => grandChild.GreatGrandChildren , () => greatGrandChild)
                .List<Parent>();

This generates the expected SQL, with lots of left outer joins. And also returns about 800 identical parents, due to the joins.

However, when i access the lists of the first parent via code in a for loop, the DB is hit countless times, completely ignoring the previous query.

Any ideas?

Info: NHibernate 3.3; DB: Oracle; VS2012 - ASP.NET

Thanks

WoF_Angel
  • 2,569
  • 9
  • 35
  • 54
  • 1
    I have the same problem, did you manage to fix this ? The strange thing is that it works with MSSQL, but not with oracle. – Robot Mess Aug 19 '13 at 15:58

1 Answers1

0

It should work with something like this:

QueryOver<Parent>()
.Where(c => c.Id == id)
.Inner.JoinQueryOver(p => p.Children)
.Inner.JoinQueryOver(c => c.GrandChild)
.Inner.JoinQueryOver(g => g.GreatGrandChildren)
.Fetch(p => p.Children).Eager
.Fetch(p => p.Children.GrandChild).Eager
.Fetch(p => p.Children.GrandChild.GreatGrandChildren).Eager
.TransformUsing(Transformers.DistinctRootEntity)

Fetch.Eager tells NH to load those nested collection within one request, TransformUsing basically tells it you want a distinct on Parent

Thomas Radioyes
  • 249
  • 2
  • 6