Search code examples
javajpajpql

Can someone explain this JPQL query for me?


What is this JPQL query?

SELECT e
FROM Department d JOIN d.employees e JOIN e.projects p
WHERE 
e.salary = :amount and 
d.name = :name and 
p.location = :location"

I understand the the first JOIN in which the Department gets joined by Employee entity but what's the reason for the second join while we are just selecting e?


Solution

  • Joining to the projects of departments is necessary, because single project is later on needed in WHERE clause:

    p.location = :location
    

    Projects is a collection. Consequently e.projects is collection valued path expression. It is not possible navigate through collection valued path expression. Following construct is not expected to work, because collection does not have location property.

    e.projects.location = :location