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

前端 未结 2 1250
忘了有多久
忘了有多久 2021-01-16 05:36

I’m using JPA 2.1, Hibernate 4.3.6.Final, and MySQL 5.5.37. How do I write a JPQL query that does a join? I’m trying below

    final String jpqlQuery = \"S         


        
2条回答
  •  迷失自我
    2021-01-16 06:04

    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.

提交回复
热议问题