如何用JPA编写这种复杂的联接查询,我在下面指出的某些语法不适用于JPA。我用它们来演示sql查询应建立的方式,对此感到抱歉。

SELECT Result1.name1, Result1.count1, Result2.name2, Result2.count2 FROM (
    SELECT
        taskOne.user.name AS name1,
        COUNT(taskOne.taskId) AS count1
    FROM
        Task AS taskOne
    INNER JOIN
        taskOne.defect AS defectOne
    WHERE (
        defectOne.defId = taskOne.defect.defId
        AND
        taskOne.taskCategory.tcaId = 1
    )
    GROUP BY
        taskOne.user.usId
) AS Result1
FULL JOIN (
    SELECT
        taskTwo.user.name AS name2,
        COUNT(taskTwo.taskId) AS count2
    FROM Task AS taskTwo
    INNER JOIN taskTwo.defect AS defectTwo
    WHERE (
        defectTwo.defId = taskTwo.defect.defId
        AND
        taskTwo.taskCategory.tcaId = 2
    )
    GROUP BY taskTwo.user.usId
)
AS Result12
WHERE Result1.name1 = Result12.name2

最佳答案

JPQL是选择对象所需要的。您的查询似乎异常复杂,我建议您使用本机SQL查询或对其进行简化。

JPQL不支持from子句中的子选择。

关于java - 如何使用JPA编写复杂的联接查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6992004/

10-10 04:59