我想将此 SQL 查询重写为 JPQL 并使用 JPA Projection:
SELECT count(id) as count, status, error_class, error_message, id, settlement_status_raw
FROM `payment_transactions`
WHERE `payment_transactions`.`terminal_id` = 16
AND (created_at > '2019-06-01 00:00:00.000000')
AND (`payment_transactions`.`status` != 'approved')
GROUP BY `payment_transactions`.`error_message` ORDER BY count DESC
我试过这个:
@Query(value = "SELECT new org.plugin.service.PaymentTransactionsDeclineReasonsDTO(e.id, count(id) as e.count, e.status, e.error_class, e.error_message) " +
" FROM payment_transactions e " +
" WHERE e.terminal_id = :id AND (e.created_at > :created_at) " +
" AND (e.status != 'approved') " +
" GROUP BY e.error_message " +
" ORDER BY e.count DESC")
但我得到错误:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '.' near line 1, column 96 [SELECT new org.plugin.service.PaymentTransactionsDeclineReasonsDTO(e.id, count(id) as e.count, e.status, e.error_class, e.error_message) FROM payment_transactions e WHERE e.terminal_id = :id AND (e.created_at > :created_at) AND (e.status != 'approved') GROUP BY e.error_message ORDER BY e.count DESC]"}}
你能给我一些建议如何正确地用 JPQL 重写这个查询吗?
最佳答案
我同意 Mike.F e.count
不是有效的表达式。
但该语句有更多问题:
e.id
, e.status
, e.error_class
不是 GROUP BY
的一部分所以他们不能在选择中使用。要么将它们添加到 GROUP BY
或使用聚合函数,如 MAX
或 MIN
. 关于java - QuerySyntaxException : expecting CLOSE, 发现 '.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57149382/