Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jpa namedquery with left join fetch

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?

like image 779
user3296520 Avatar asked Jul 21 '14 09:07

user3296520


1 Answers

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"
)
like image 130
Roman C Avatar answered Oct 11 '22 07:10

Roman C