首页 > 解决方案 > 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 重写这个查询吗?

标签: javajpaspring-data-jpajpa-2.0jpa-2.1

解决方案


我同意 Mike.Fe.count不是一个有效的表达方式。但该声明还有更多问题:

  • e.id, e.status,e.error_class不是其中的一部分,GROUP BY因此它们不能在选择中使用。将它们添加到GROUP BY或使用聚合函数,例如MAXMIN

  • 您不能在构造函数表达式中定义别名。


推荐阅读