带有左连接提取的 Jpa 命名查询 [英] Jpa namedquery with left join fetch

查看:30
本文介绍了带有左连接提取的 Jpa 命名查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的命名查询:

@NamedQuery(name = "User.findOneWithLists",query = "SELECT u FROM User u "+ "LEFT JOIN FETCH u.aTemplates "+ "LEFT JOIN FETCH u.bTemplates "+左连接获取u.bp"+LEFT JOIN FETCH u.aCredentials"+ "LEFT JOIN FETCH u.st WHERE (st.deleted = false) "+LEFT JOIN FETCH u.bCredentials"+左连接获取u.cl"+ "在哪里 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:意外标记:LEFT ....

在st侧有一个注释

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

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

Any idea how can I handle this where clause?

推荐答案

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

Check a SQL syntax, you can't use left join after where clause. If you are looking at the SQL generated form that named query you will see that joined tables in the query the where clause 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.

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

这篇关于带有左连接提取的 Jpa 命名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆