2011-08-09 85 views
1

如何使用JPA編寫這種複雜的連接查詢,下面指出的一些語法不適用於JPA。我已經用它們來演示sql查詢應該如何構建的方式,所以很抱歉。如何使用JPA編寫複雜的連接查詢

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 

回答

2

JPQL是選擇對象的理想選擇。你的查詢看起來非常複雜,我會推薦一個原生的SQL查詢,或者簡化它。

JPQL不支持from子句中的子選擇。

+0

嗨,詹姆斯!非常感謝反饋,是的,目前我使用本機查詢爲「entityManager.createNativeQuery(」sqlQuery「)」。玩得開心! – Channa