JPA标准查询:使用实体的左联接子查询结果

时间:2018-07-05 11:33:36

标签: java sql jpa eclipselink criteria-api

我们有一个复杂的本机查询,该查询涉及带有子查询结果的左联接。该查询需要转换为条件查询。以下是查询:

SELECT RP.reportid, RP.reportname, T.terminalname, RP.creationdate, U.fullusername as createdby, L.languagename, B.buildname, R.releasename, RPMI.protocolname, RPMI.setupconfigname as setupconfigname, coalesce(COUNT(IF(S.statusname='Passed', E.executionid, NULL)), 0) as passed, coalesce(COUNT(IF(S.statusname='Failed', E.executionid, NULL)), 0) as failed, coalesce(COUNT(IF(S.statusname='Not Executed', E.executionid, NULL)), 0) as notexecuted, COUNT(E.executionid) as totalExecutions, COUNT(DISTINCT E.testcaseid) as totalTestcases, RPMI.attributevalue as executiontype
FROM reports RP
LEFT JOIN executions E ON E.reportid = RP.reportid
LEFT JOIN status S ON S.statusid = E.statusid
JOIN builds B ON B.buildid = RP.buildid
JOIN terminals T ON T.terminalid = RP.terminalid
JOIN languages L ON L.languageid = RP.languageid
JOIN releases R ON R.releaseid = B.releaseid
JOIN projects P ON P.projectid = R.projectid
JOIN users U ON U.userid = RP.createdby
LEFT JOIN (
    SELECT RPI.reportid, RPM_reporttype.attributevalue as executiontype, RPM_session.attributevalue, PRT.protocolid, PRT.protocolname, SCT.simconfigtypeid as setupconfigid, SCT.simconfigtypename as setupconfigname
    FROM reports RPI
    LEFT JOIN reportparam RPM_proto ON RPM_proto.reportid = RPI.reportid AND RPM_proto.attributename="protocolid"
    LEFT JOIN reportparam RPM_simconf ON RPM_simconf.reportid = RPI.reportid AND RPM_simconf.attributename ="simconfigid"
    LEFT JOIN reportparam RPM_reporttype ON RPM_reporttype.reportid = RPI.reportid AND RPM_reporttype.attributename = "reporttype"
    LEFT JOIN protocols PRT ON PRT.protocolid = RPM_proto.attributevalue
    LEFT JOIN simconfigtypes SCT ON SCT.simconfigtypeid = RPM_simconf.attributevalue
) as RPMI ON RPMI.reportid = RP.reportid
WHERE RP.flag = 10
AND P.projectid = '34'
GROUP BY RP.reportid;

问题:

  1. 用于子查询的标准API不允许多项选择

  2. 如何使用实体对象(在本例中为Report实体)完成子查询结果的左联接?

注意:我们正在使用JPA的EclipseLink实现。

0 个答案:

没有答案