Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用count实现JPA投影_Java_Jpa_Spring Data Jpa_Jpa 2.0 - Fatal编程技术网

Java 用count实现JPA投影

Java 用count实现JPA投影,java,jpa,spring-data-jpa,jpa-2.0,Java,Jpa,Spring Data Jpa,Jpa 2.0,我想用count实现JPA投影。我试过这个: @Query(value = "SELECT new org.service.PaymentTransactionsDeclineReasonsDTO( id, count(id) as count, status, error_class, error_message) " + " FROM payment_transactions " + " WHERE terminal_id = :id AND (created

我想用count实现JPA投影。我试过这个:

@Query(value = "SELECT new org.service.PaymentTransactionsDeclineReasonsDTO( id, count(id) as count, status, error_class, error_message) " +
        " FROM payment_transactions " +
        " WHERE terminal_id = :id AND (created_at > :created_at) " +
        " AND (status != 'approved') " +
        " GROUP BY error_message " +
        " ORDER BY count DESC", nativeQuery = true)
List<PaymentTransactionsDeclineReasonsDTO> transaction_decline_reasons(@Param("id") Integer transaction_unique_id, @Param("created_at") LocalDateTime created_at);
@Query(value=“选择新建组织.service.PaymentTransactionDeclineReasonsTo(id,计数(id)作为计数,状态,错误类,错误消息)”+
“来自付款交易”+
“其中终端\u id=:id和(在>上创建的:在创建的)”+
“和(状态!=‘已批准’)”+
“按错误消息分组”+
“按计数说明排序”,nativeQuery=true)
列出事务拒绝的原因(@Param(“id”)整数事务唯一的\u id、@Param(“created\u at”)LocalDateTime created\u at);
但是我得到了错误:
原因:java.sql.SQLException:您的sql语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第1行使用“.plugin.service.PaymentTransactionDeclineReasonsTo(id,计数(id)为c”的正确语法


当我使用基于类的投影时,如何实现正确的计数?

您混合了JPQL和SQL语法

构造函数表达式(
new…
)是JPQL,但在注释中,您将其标记为
nativeQuery
,即SQL,因此您必须下定决心

如果它是SQL,我认为您不能在
ORDER BY
子句中使用别名,因此您可能必须重复表达式或将其包装在一个子选择中,如下所述:

如果是JPQL,它不支持构造函数表达式中的别名,因此我想您必须在order by子句中重复该表达式。

尝试而不是DTO:

公共接口事务declineReason{
整数getId();
长getCount();
状态getStatus();
ErrorClass getErrorClass();//我想是枚举。。。
字符串getErrorMessage();
}
@Query(value=“select”+
“id作为id,”+
“计数(id)为计数,”+
“状态为状态,”+
“error_类作为errorClass,”+
“错误消息作为错误消息”+
“来自”+
“付款交易”+
“哪里”+
“终端标识=?1”+
“并在>2处创建了_”+
“和状态!=‘已批准’”+
“集团”+
“通过错误消息”+
“订购人”+
“2描述”,nativeQuery=true)
列出getTransactionDeclineReasons(整数事务ID、LocalDateTime createdAt);

请注意别名(即,
id as id
)-它们是必需的。

删除=和:之间的空格,如下
其中终端id=:id和(创建时>:created时)”+
是否确定?错误是针对计数(id)作为第1行的“c”?一旦我遇到类似的错误并通过这样做解决了,你可以尝试。同时删除>&:我卡住了。你能帮我一下吗?