Why do I get an invalid path when I try and construct a JPQL join query?

给你一囗甜甜゛ 提交于 2019-12-01 11:49:21

The issue reported by Hibernate is in the first row:

final String jpqlQuery = "SELECT m FROM Message m LEFT JOIN MessageReadDate mr "...

in the LEFT JOIN statement. In hql, the JOIN must be expressing the relation, e.g.

LEFT JOIN m.MessageReadDate mr // the m is referencing the MessageReadDate

If there is no reference, we can still use that, but with Cartesian product

FROM Message m, MessageReadDate mr

in that case, the CROSS JOIN will be issued

See the:

small cite:

Multiple classes can appear, resulting in a cartesian product or "cross" join.

from Formula, Parameter
from Formula as form, Parameter as param

You can also assign aliases to associated entities or to elements of a collection of values using a join. For example:

from Cat as cat
    inner join cat.mate as mate

If you want to use JOIN's in HQL then you have to use it only on the properties of the class which is mentioned in From clause.

What it means is, in this particular line:

SELECT m FROM Message m LEFT JOIN MessageReadDate mr 

hibernate checks if MessageReadDate is defined as a property in Message class which is considered as path here. As there is nothing called MessageReadDate it throws an exception.

So to fix this issue, add the required property for MessageReadDate class in Message class, lets say mrd (you can have your relation as one-to-one or on-to-many or etc) and then use the query like this:

SELECT m FROM Message m LEFT JOIN m.mrd

in this way we are telling hibernate how to join the message with its corresponding properties so the path for join is clear to hibernate.

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