HQL ERROR: Path expected for join

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

I keep trying variations of this query and can't seem to make this happen. I've also referenced this post: Path Expected for Join! Nhibernate Error and can't seem to apply the same logic to my query. My User object has a UserGroup collection.

I understand that the query needs to reference entities within the object, but from what I'm seeing I am...

@NamedQuery(   name = "User.findByGroupId",   query =     "SELECT u FROM UserGroup ug " +     "INNER JOIN User u WHERE ug.group_id = :groupId ORDER BY u.lastname" ) 

回答1:

select u from UserGroup ug inner join ug.user u  where ug.group_id = :groupId  order by u.lastname 

As a named query:

@NamedQuery(   name = "User.findByGroupId",   query =     "SELECT u FROM UserGroup ug " +     "INNER JOIN ug.user u WHERE ug.group_id = :groupId ORDER BY u.lastname" ) 

Use paths in the HQL statement, from one entity to the other. See the Hibernate documentation on HQL and joins for details.



回答2:

You need to name the entity that holds the association to User. For example,

... INNER JOIN ug.user u ... 

That's the "path" the error message is complaining about -- path from UserGroup to User entity.

Hibernate relies on declarative JOINs, for which the join condition is declared in the mapping metadata. This is why it is impossible to construct the native SQL query without having the path.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!