Java 带有左连接提取的 Jpa 命名查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24862058/
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-08-14 15:16:16  来源:igfitidea点击:

Jpa namedquery with left join fetch

javahibernatejpaannotationsnamed-query

提问by user3296520

this is my namedquery:

这是我的命名查询:

@NamedQuery( name = "User.findOneWithLists", query = "SELECT u FROM User u " + "LEFT JOIN FETCH u.aTemplates " + "LEFT JOIN FETCH u.bTemplates " + "LEFT JOIN FETCH u.bp " + "LEFT JOIN FETCH u.aCredentials " + "LEFT JOIN FETCH u.st WHERE (st.deleted = false) " + "LEFT JOIN FETCH u.bCredentials " + "LEFT JOIN FETCH u.cl " + "WHERE u.id= :id")

@NamedQuery( name = "User.findOneWithLists", query = "SELECT u FROM User u " + "LEFT JOIN FETCH u.aTemplates" + "LEFT JOIN FETCH u.bTemplates" + "LEFT JOIN FETCH u.bp " + "LEFT JOIN FETCH u.aCredentials" + "LEFT JOIN FETCH u.st WHERE (st.deleted = false)" + "LEFT JOIN FETCH u.bCredentials" + "LEFT JOIN FETCH u.cl" + "WHERE u.id= :id ”)

My problem is that I get an error when the application starting:

我的问题是应用程序启动时出现错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: LEFT ....

org.hibernate.hql.internal.ast.QuerySyntaxException:意外标记:LEFT ....

On the st side there is an annotation

在st侧有一个注释

@ManyToOne
 @JoinColumn(name = "st_user")
 private User user;

Any idea how can I handle this where clause?

知道如何处理这个 where 子句吗?

采纳答案by Roman C

Check a SQL syntax, you can't use left joinafter whereclause. If you are looking at the SQL generated form that named query you will see that joined tables in the query the whereclause comes after joins and should specify equal condition that links those tables by the keys. The primary key of the main table on the left and the foreign key of the joined table on the right. The joined table is specified by the property of your many-to-one association.

检查 SQL 语法,不能使用left joinafterwhere子句。如果您正在查看命名查询的 SQL 生成表单,您将看到查询中的联接表,该where子句出现在联接之后,并且应指定通过键链接这些表的相等条件。左边是主表的主键,右边是连接表的外键。连接表由多对一关联的属性指定。

@NamedQuery(
    name = "findOneWithLists",
    query = "from Table t left join User u where u.id= :id"
)

回答by Thomas

For join conditions Hibernate provides the withkeyword, even before JPA 2.1.

对于连接条件 Hibernate 提供了with关键字,甚至在 JPA 2.1 之前。

The relevant part of your query thus would look like this:

因此,您查询的相关部分如下所示:

SELECT u FROM User u  ... LEFT JOIN u.st WITH st.deleted = false

I'm not sure about LEFT JOIN FETCH u.cl with u.id= :idbut if I remember correctly, that's not as easy and might have to be resolved with an adapted join and u.ui = :idin the where condition.

我不确定,LEFT JOIN FETCH u.cl with u.id= :id但如果我没记错的话,这并不容易,可能必须通过调整连接和u.ui = :idwhere 条件来解决。

LEFT JOIN FETCHu.st WITH st.deleted = false`

LEFT JOIN FETCHu.st WITH st.deleted = false`

This is not supported, since you can't do a partial fetch.

这不受支持,因为您不能进行部分提取。