2
votes

I'm trying to get nhibernate to use a alias in a eager fetch. Im not sure its possible.

I'm trying to use the alias (bAlias in my example) in my fetch.

QueryOver<A>() 
.JoinAlias(x => x.B, () => bAlias) 
.JoinAlias(x => x.B, () => bAlias2) 
.Where(() => bAlias2.Surname == "Smith") 
.Fetch(() => bAlias).Eager 
.Fetch(() => bAlias.C).Eager; 

As you can see the fetch commands are using the 2 alias's instead of the direct path from .

The above code doesn't work. The code that does work is

QueryOver<A>() 
.JoinAlias(x => x.B, () => bAlias) 
.JoinAlias(x => x.B, () => bAlias2) 
.Where(() => bAlias2.Surname == "Smith") 
.Fetch(x => x.B).Eager 
.Fetch(x => x.B.C).Eager;

As you can see its the Fetch statements that are different.

2
Why do you need to use the alias from JoinAlias? - Andrew Whitaker

2 Answers

3
votes

Try something like this:

B b = null;

QueryOver<A>()
   .Fetch(x => x.B).Eager
   .JoinAlias(x => x.B, () => b, JoinType.LeftOuterJoin);
0
votes

You can just use a join instead, Fetch resolves to INNER JOIN LEFT OUTER JOIN anyway.

QueryOver<A>() 
    .JoinAlias(x => x.B, () => bAlias) 
    .Where(() => bAlias.Surname == "Smith") 
    .Fetch(x => x.B).Eager 
    .JoinQueryOver(() => bAlias.C)