Jpa用左连接获取命名查询 [英] Jpa namedquery with left join fetch

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

问题描述

这是我的namedquery:

blockquote
@NamedQuery(
name =User.findOneWithLists,
query =选择你从用户u
+左加入FETCH u.a模板
+左联合FETCH u.b模板
+左联合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)


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



另一方面有一个注释

  @ManyToOne 
@JoinColumn(name =st_user)
私人用户用户;

任何想法如何处理这个where子句?

之后使用 left join c $ c>子句。如果您正在查看命名查询的SQL生成表单,那么您将在查询中看到其中子句在连接之后出现的连接表,并应指定将这些表链接到按键。左边主表的主键和右边连接表的外键。加入的表由您的多对一关联的属性指定。

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


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")

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

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

On the st side there is an annotation

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

Any idea how can I handle this where clause?

解决方案

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天全站免登陆