Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate 3. Alternatives to "ThenFetch" in QueryOver

I'm using NHibernate 3.0 with both the LINQ provider and QueryOver. Sometimes I want to eager load related data, and there comes the method "Fetch" to the rescue, both in LINQ and QueryOver. Now I have a special scenario where I want to eager load a property not directly on the second level, like:

Foo f = ...;
f.A.B.C

with LINQ there's no problem, as you can "chain" fetches with the method "ThenFetch", like:

var result = Session.Query<Foo>().Fetch(a => a.A).ThenFetch(b => b.B).ThenFetch(c => c.C).ToList();

In QueryOver there's no such method, so how can I achieve the same result?

Thanks in advance.

like image 395
psousa Avatar asked Jan 26 '11 03:01

psousa


1 Answers

I actually managed to solve this problem using two different approaches:

Approach one:

Session.QueryOver<Foo>().Fetch(x => x.A).Fetch(x => x.A.B).Fetch(x => x.A.B.C)

Approach two:

A a = null;
B b = null;
C c = null;

Session.QueryOver<Foo>()
    .JoinAlias(x => x.A, () => a)
    .JoinAlias(() => a.B, () => b)
    .JoinAlias(() => b.C, () => c)

Both work (altough I'm not exactly sure if one of them generated "inner" and the other one "outer" joins).

like image 119
psousa Avatar answered Sep 21 '22 18:09

psousa