JPA Hibernate JOIN ... ON ... 和

JPA Hibernate JOIN ... ON ... AND

我使用以下查询:

SELECT * FROM phaseinproject as pip JOIN projectinrelease pir 
ON pip.projectInRelease_id = pir.id
JOIN releaseperiod as rp ON pir.release_id = rp.id
JOIN releasestructure as rs ON rs.id = rp.releaseStructure_id
JOIN phaseinreleasestructure as pirs ON pirs.releaseStructure_id = rs.id
JOIN releasephase as rlp ON rlp.id = pirs.phase_id 
AND rlp.id = pip.phase_id 

这个查询完全没问题。我得到三个结果(我期望的数量)。

我将此查询转换为以下 HQL 查询:

            TypedQuery<PhaseInProjectOverview> findPhasesInRelease = em.createQuery("SELECT NEW nl.dashboard.dto.out.PhaseInProjectOverview(phaseInProject.id, phase.name, phaseInProject.startDate, phaseInProject.plannedEndDate, phaseInProject.endDate) FROM PhaseInProject phaseInProject "
                + "JOIN phaseInProject.projectInRelease projectInRelease "
                + "JOIN projectInRelease.release release "
                + "JOIN release.releaseStructure releaseStructure "
                + "JOIN releaseStructure.phaseInReleaseStructures phaseInReleaseStructure "
                + "JOIN phaseInReleaseStructure.phase phase "
                + "WHERE release.id = :releaseId ORDER BY phaseInReleaseStructure.position, phaseInProject.startDate", PhaseInProjectOverview.class);
        findPhasesInRelease.setParameter("releaseId", releaseId);
        return findPhasesInRelease.getResultList();

无论我尝试什么:我得到 6 个结果,因为 HQL 似乎不支持 "JOIN ... ON ... AND ..." sql 语法。

有谁知道如何解决这个问题?

编辑: 我用使用过的解决方案添加了自己的答案。谢谢大家的 answers/pointers.

尝试使用 with 关键字:phaseInReleaseStructure.phase phase WITH phase.id = phaseInProject.phase_id - 这应该会导致 SQL,例如 releasephase as rlp ON rlp.id = pirs.phase_id AND rlp.id = pip.phase_id

或者只需在 where 子句中添加该条件:

... WHERE release.id = :releaseId AND phase.id = phaseInProject.phase_id ... 

我用一个额外的 WHERE 子句解决了我的问题:

phase.id = phaseInProject.phase.id

现在我得到了我期待的结果。 'WITH' 关键字似乎不适用于多个实体。当我尝试时,出现异常:

HQL error: with-clause referenced two different from-clause elements

当尝试使用像 JOIN phaseInReleaseStructure.phase phase ON phase.id = phaseInProject.id 这样的 'ON' 语法时,我得到另一个错误:

unexpected token: ON near line 1, column 473