java Hibernate Left Outer Join 问题:加入时需要路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3915404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:57:42  来源:igfitidea点击:

Hibernate Left Outer Join problem: path expected on join

javahibernateormhqlleft-join

提问by Nivas

I have two tables, something like:
Article and ArticleRelevance
They have a one to one relationship, and ArticleRelevance is optional, meaning a Article may (or may not) have a ArticleRelevance.

我有两个表,比如:
Article 和 ArticleRelevance
它们是一对一的关系,ArticleRelevance 是可选的,这意味着文章可能(也可能没有)有一个 ArticleRelevance。

I want to select all articles along with the ArticleRelevance details.

我想选择所有文章以及 ArticleRelevance 详细信息。

With traditional SQL, I will do a outer join on the tables, like

使用传统的 SQL,我将对表进行外部联接,例如

SELECT *
FROM ARTICLE A LEFT OUTER JOIN ARTICLE_RELEVANCE AR ON A.ARTICLE_ID = AR.ARTICLE_ID

I tried a similar query in HQL and got a Exception with message "Path Expected for Join"

我在 HQL 中尝试了一个类似的查询并得到了一个带有消息的异常 "Path Expected for Join"

I am unable to understand this message. What does this mean, and how do I solve it?

我无法理解此消息。这是什么意思,我该如何解决?

(As you probably have already guessed) I am an absolute hibernate newbie.

(您可能已经猜到了)我是一个绝对的休眠新手。

I haveto use HQL. Actually I have an HQL already, joining two or three tables, and I have to add this condition.

必须使用 HQL。其实我已经有一个HQL,加入了两三个表,我必须添加这个条件。

StringBuffer hql = new StringBuffer();
hql.append(" select new service.orders.Order (order.orderNo, article.articleNo, article.articleName)");
hql.append(" from (Order order join  Article article with order.articleNo = article.articleNo) left outer join  ArticleRelevance articleRelevance with article.articleNo = articleRelevance.articleNo");
hql.append(" and order.orderNo =  "+orderNumber);

In thisforum, someone says this is "missing functionality". True?

这个论坛中,有人说这是“缺少功能”。真的?

回答by Pascal Thivent

I tried a similar query in HQL and got a Exception with message "Path Expected for Join"

我在 HQL 中尝试了一个类似的查询,并得到了一个消息“Path Expected for Join”的异常

With HQL, a [ LEFT [OUTER] | INNER ] JOINis done along an association path (constructed with an identification variable followed by a dot and an association field). So something like this in your case:

使用 HQL,a[ LEFT [OUTER] | INNER ] JOIN沿着关联路径完成(由标识变量、后跟点和关联字段构成)。所以在你的情况下是这样的:

from Article a left join a.articleRelevance

The keyword OUTER is optional.

关键字 OUTER 是可选的。

And your bigger query becomes:

你更大的查询变成:

select new service.orders.Order(order.orderNo, article.articleNo, article.articleName)
from Order order 
join order.article article
left outer join article.articleRelevance
where order.orderNo = :orderNo

Note the use of a named parameter:orderNo(don't concatenate parameters). Use it like this:

注意命名参数的使用:orderNo(不要连接参数)。像这样使用它:

String queryString = "...";
Query q = session.createQuery(queryString);
q.setParameter("orderNo", orderNumber);
List<Order> results = q.list();

References

参考