I am struggling to convert this sql query
SELECT feature.*, rolefeature.roleId, rolefeature.permission
FROM feature
Left JOIN rolefeature
ON feature.featureId = roleFeature.featureId
and roleFeature.roleId = ?
which works fine.
to JPQL. I have this
select f, rf.role.roleId as roleId, rf.permission
from Feature f
LEFT JOIN f.roleFeatures rf
and rf.role.roleId = :roleId
but it seems to complain about the AND keyword. I’m at a bit of a loss. If I use a WHERE instead of an AND, it does not return the correct data
First of all, the
andclause makes no sense here. You need a where clause.Second, querying on
rf.roleimplicitely makes an inner join between RoleFeature and Role. You would see it by inspecting the generated SQL. You need to do a second left join to access Role.Third, if you put the restriction on the role ID in the where clause, the left join will become an inner join. So you need the equivalent of the
onclause in JPQL. I don’t think JPQL has that, but Hibernate supports it using thewithkeyword:Maybe you can simulate it with