NHibernate 3 LINQ 内连接问题,具有三个跳转:NotSupportedException
我有一个曾经在 NHibernate LINQ 2.1.2 中工作的查询,但它用 NH3 抛出 NotSupportedException:
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;
关系如下:
- Flower References Stem
- Stem HasMany Flowers
- Leaf References Stem
- Stem HasMany Leaves
异常是从 NHibernate.Linq 中的第 204 行抛出的.Visitors.QueryModelVisitor。这是源代码中的方法:
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);
}
在我看来,在以下线程下讨论了相同的问题:
在该线程下,Stefan 提到不支持该语法:
LINQ 提供程序期望 表达式为:
QuerySourceReferenceExpression 。会员
但是,在 from brw in 的情况下 贷款.申请.借款人是:
QuerySourceReferenceExpression 。成员 。会员
所以这绝对是不受支持的 功能。
那么,NH3 LINQ 是否随时支持此语法?我认为这是一个简单的语法,而且拥有它很好。
但是我可以通过将查询重写为来解决这个问题:
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;
顺便说一句,有人有更好的解决方法吗?
nhuusers 链接:http://groups.google.com/group/nhusers/browse_thread/thread/334a53c749b0b377
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:
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, is this syntax 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?
nhusers link: http://groups.google.com/group/nhusers/browse_thread/thread/334a53c749b0b377
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
付出所有努力后,你的问题是:
……这个论坛根本无法回答。 NHibernate 不是一个有路线图的商业产品。您不能只是在这里发帖并希望其中一位志愿者开发人员做出回应。
请记住,NHibernate 是开源的,因此社区(包括您!)拥有此类问题。
我一直在关注 nhibernate-development 列表,它看起来像 LINQ 提供程序是一个主要工作领域。但是,我不知道您的具体问题是否会得到解决。增加解决此问题的机会的最佳方法是在 NHibernate JIRA 中提交错误以及显示问题的测试用例。
如果您的具体问题看起来没有得到解决,为什么不下载源代码并尝试自己修复它,和/或在邮件列表上进一步讨论它?如果您下载源代码并稍微使用它,您还会发现它有很多很棒的示例测试用例,您可以在提交错误时将其用作示例。
After all that effort put in, your question:
... simply cannot be answered in this forum. NHibernate is not a commercial product with a roadmap. You can't just post here and hope one of the volunteer developers responds.
Remember that NHibernate is open source, so the community (including you!) owns issues like this.
I have been following the the nhibernate-development list, and it looks like the LINQ provider is a major area of work. However, I don't know if your specific issue will be addressed. The best way to increase the chances of this issue being fixed is to file a bug in the NHibernate JIRA along with a test case showing the problem.
If it doesn't look like your specific issue will be addressed, why not download the source code and try fixing it yourself, and/or discussing it further on the mailing list? If you download the source code and work with it a little, you'll also find that it has plenty of great example test cases that you can use as examples when you file the bug.
尚未测试您的确切示例,但我在 NH 3.2 中遇到了类似的问题,发现该问题已在 NH 3.3 中解决
Haven't tested your exact example but I had a similar issue in NH 3.2 and found that is has been resolved in NH 3.3