NHibernate 3 LINQ inner join issue with three jumps: NotSupportedException

223 views
Skip to first unread message

Farzad

unread,
Jan 4, 2011, 5:00:42 PM1/4/11
to nhusers
I have a query that used to work in NHibernate LINQ 2.1.2 but it is
throwing NotSupportedException with NH3:

IQueryable<Tree> query = from flower in
GetSession().Query<Flower>()
from leaf in
flower.Stem.Leaves // <--- the problem is here with three jumps
where leaf.Color == Green
select flower;

The relations are like:
- Flower References Stem
- Stem HasMany Flowers
- Leaf References Stem
- Stem HasMany Leaves

The exception is thrown from line 204 in
NHibernate.Linq.Visitors.QueryModelVisitor. Here is the method from
the source code:


public override void VisitAdditionalFromClause(AdditionalFromClause
fromClause, QueryModel queryModel, int index)
{
if (fromClause is LeftJoinClause)
{
// It's a left join
_hqlTree.AddFromClause(_hqlTree.TreeBuilder.LeftJoin(

HqlGeneratorExpressionTreeVisitor.Visit(fromClause.FromExpression,
VisitorParameters).AsExpression(),

_hqlTree.TreeBuilder.Alias(fromClause.ItemName)));
}
else if (fromClause.FromExpression is MemberExpression)
{
var member = (MemberExpression) fromClause.FromExpression;

if (member.Expression is QuerySourceReferenceExpression)
{
// It's a join
_hqlTree.AddFromClause(_hqlTree.TreeBuilder.Join(

HqlGeneratorExpressionTreeVisitor.Visit(fromClause.FromExpression,
VisitorParameters).AsExpression(),

_hqlTree.TreeBuilder.Alias(fromClause.ItemName)));
}
else
{
// What's this?
throw new NotSupportedException(); // <-----------------------
LINE 204
}
}
else
{
// TODO - exact same code as in MainFromClause; refactor this out
_hqlTree.AddFromClause(_hqlTree.TreeBuilder.Range(

HqlGeneratorExpressionTreeVisitor.Visit(fromClause.FromExpression,
VisitorParameters),

_hqlTree.TreeBuilder.Alias(fromClause.ItemName)));

}

base.VisitAdditionalFromClause(fromClause, queryModel, index);
}


It seems to me the same issue is discussed under the following thread:

http://groups.google.com/group/nhusers/browse_thread/thread/dbceb7eb1e31f027/f8e69671b750e0d6?lnk=gst&q=NotSupportedException+stefan#f8e69671b750e0d6

Under that thread Stefan mentions that the syntax is not supported:
The LINQ provider expects the expression to be:
<QuerySourceReferenceExpression> . <Member>
However, in the case of from brw in
loan.Application.Borrowers it is:
<QuerySourceReferenceExpression> . <Member> . <Member>
So it's definately an unsupported feature.

So the question is if this syntax is going to be supported anytime in
NH3 LINQ? I think it is a trivial syntax and it's good to have.

However I can go around this issue by rewriting the query as:

IQueryable<Tree> query = from stem in
GetSession().Query<Stem>()
from leaf in
stem.Leaves
from flower in
stem.Flowers
where leaf.Color ==
Green
select flower;

BTW, anyone has a better workaround?

Stefan Wenig

unread,
Jan 5, 2011, 3:45:10 AM1/5/11
to nhusers
be brave! try what I said, add a test or two, and if it works, send a
patch!
> http://groups.google.com/group/nhusers/browse_thread/thread/dbceb7eb1...
Reply all
Reply to author
Forward
0 new messages