【发布时间】:2019-11-30 12:24:54
【问题描述】:
我想用计数来实现 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);
但我收到错误:Caused by: java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.plugin.service.PaymentTransactionsDeclineReasonsDTO( id, count(id) as c' at line 1
当我有基于类的投影时如何实现正确的计数?
【问题讨论】:
-
删除 = 和 : 之间的空格,如下
WHERE terminal_id =:id AND (created_at >:created_at) " + -
你确定吗?错误在于第 1 行的 count(id) as c'?
-
一旦我遇到类似的错误并通过这样做解决了,你可以试试。也删除 > & 之间的空格:
标签: java jpa spring-data-jpa jpa-2.0