Postgres查询到JPA转换

时间:2016-01-15 06:37:51

标签: java postgresql jpa

我有一个postgres查询,我想用JPA执行。

任何人都可以告诉我如何在jpa

中进行正确的查询
SELECT  (
        SELECT COUNT(*)
        FROM "RECEIPT" Where "STATUS"='1'
        ) AS count1,
        (
        SELECT COUNT(*)
        FROM   "RECEIPT"  Where "STATUS"='1'
        ) AS count2;

返回

count1 count2
11      11

在jpa中,我试着跟随。

Query query = em.createQuery("SELECT (SELECT count(*)  FROM SMSReceipt Where status='1' ) AS count1,(SELECT count(*)  FROM SMSReceipt Where status='8' ) AS count2 ");

但这不起作用

请建议我怎么做

1 个答案:

答案 0 :(得分:1)

您需要使用createNativeQuery()方法来运行纯SQL

Query query = em.createNativeQuery("SELECT (SELECT count(*)  FROM SMSReceipt Where status='1' ) AS count1,(SELECT count(*)  FROM SMSReceipt Where status='8' ) AS count2 ");

createQuery()用于运行JPQL。

同时检查 - JPA's EntityManager createQuery() vs createNamedQuery() vs createNativeQuery()