我希望实现JPA投影并计数。我尝试了以下代码:
Original Answer 翻译成“最初的回答”:
@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
当我拥有基于类的Projection时,我该如何实现正确的计数?
最初的回答:
您的SQL语句中存在语法错误。在使用基于类的Projection时,如果要进行计数操作,请确保您的SQL语法正确。请参考MariaDB服务器版本的手册以获得正确的语法。
WHERE terminal_id=:id AND (created_at>:created_at) " +
- TheSprinter