java - QuerySyntaxException:期待关闭,找到“。”
问题描述
我想将此 SQL 查询重写为 JPQL 并使用 JPA 投影:
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.Fe.count
不是一个有效的表达方式。但该声明还有更多问题:
e.id
,e.status
,e.error_class
不是其中的一部分,GROUP BY
因此它们不能在选择中使用。将它们添加到GROUP BY
或使用聚合函数,例如MAX
或MIN
。您不能在构造函数表达式中定义别名。
推荐阅读
- c# - 如何仅在使用 SQL 服务器的数据库填充的 datagridview 中显示日期?
- android - 检查一个值是否为空
- mysql - 带有输入和输出 SQL 的存储过程
- caching - Wildfly infinispan Web 缓存容器有什么用?
- grafana - 循环数据到 grafana
- java - ORA-29531: 类错误中没有方法
- python - sklearn / CountVectorizer : 适合然后变换不同的输入语料库
- javascript - 在输入 Javascript 时为单个字符着色
- java - JENKINS:无法设置 org.eclipse.aether.spi.log.Logger 字段 org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider.logger
- c# - AWS TranslateText REST API 调用添加签名 v4