如何用JPA编写复杂的连接查询

时间:2011-08-09 06:03:43

标签: java jpa jpql

如何使用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

1 个答案:

答案 0 :(得分:3)

选择对象需要JPQL。您的查询看起来非常复杂,我建议使用本机SQL查询,或简化它。

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