我使用以下查询:

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语法。

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

编辑:
我在使用的解决方案中添加了自己的答案。谢谢大家的答案/指标。

最佳答案

尝试with关键字:phaseInReleaseStructure.phase phase WITH phase.id = phaseInProject.phase_id-这应该导致像releasephase as rlp ON rlp.id = pirs.phase_id AND rlp.id = pip.phase_id这样的SQL

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

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

08-28 18:52