如何在HQL中的HAVING COUNT()中使用CASE?

时间:2015-12-14 18:45:55

标签: java hibernate case hql having

我在PostgreSQL客户端控制台中测试了以下SQL查询,该工作正常:

SELECT document_url FROM signature_transaction WHERE app_id = 2 GROUP BY document_url HAVING COUNT(CASE WHEN response_delivered THEN 1 ELSE 0 END) = COUNT(id);

然后我尝试使用像这样的NamedQuery在Java代码中做同样的事情:

SELECT t.documentUrl FROM SignatureTransaction t WHERE t.appId = :appId GROUP BY t.documentUrl HAVING COUNT(CASE WHEN t.responseDelivered THEN 1 ELSE 0 END) = COUNT(t.id)

但是在部署应用程序时收到以下错误:

ERROR [org.hibernate.internal.SessionFactoryImpl] (ServerService Thread Pool -- 86) HHH000177: 
Error in named query: SignatureTransaction.findUrlOfDeliveredDocuments: 
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: responseDelivered near line 1, column 133 
[SELECT t.documentUrl FROM org.app.model.SignatureTransaction t WHERE t.appId = :appId GROUP BY t.documentUrl HAVING COUNT(CASE WHEN responseDelivered THEN 1 ELSE 0 END) = COUNT(t.id)]

我试图在COUNT()中使用所有这些名称,没有工作:

  • responseDelivered
  • t.responseDelivered
  • response_delivered
  • org.app.model.SignatureTransaction.responseDelivered

编写此HQL的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

尝试检查SignatureTransaction实体是否具有responseDelivered字段并正确设置其名称。注意HSQL在实体中看不到表名列的名称字段,这很重要。