简体   繁体   中英

Criteria API create query with complex where clause

Could someone suggest me how to write this query using the Criteria API, please?

SELECT CM.REL AS GROUP_ID, COUNT(DISTINCT C.ID) AS IDS_FOUND
FROM PROBLEMS C
         INNER JOIN PROBLEMS.ITEM CM
                    ON C.ID = CM.PROBLEM_ID
WHERE (
       CM.REL IS NOT NULL AND CM.REL = 'FF' AND C.ID NOT IN
           (SELECT DISTINCT C.ID
            FROM PROBLEMS C
                  INNER JOIN PROBLEMS.ITEM CM
                        ON C.ID = CM.PROBLEM_ID
            WHERE CM.REL IS NOT NULL AND CM.REL = 'RR')
OR
   (CM.REL IS NOT NULL AND CM.REL = 'RR')
)
GROUP BY CM.REL

JPQL and Criteria API are better restricted to simple CRUDs. For instance, your 'Service' layer component may want to find and interact with a bunch of related entities, manipulate and save state. JPQL/Criteria API/find() are better suited for usecases of that nature.

If you are looking to extract data using complex queries (typical of reporting and presentation requirements), you may want to look into using @Immutable and @Subselect over SQL query or view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM