2015-12-14 42 views
1

我已經測試了PostgreSQL客戶端控制檯,它工作正常,下面的SQL查詢:如何在HQL中使用COUNT()中的CASE?

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); 

然後我試圖做同樣的事情在Java代碼中,使用這樣的NamedQuery:

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的正確方法?

回答

0

嘗試檢查SignatureTransaction實體是否具有responseDelivered字段,並且它的名稱設置是否正確。注意HSQL沒有在實體中看到名稱列的表名字段,這很重要。

+0

該字段聲明爲@Column(name =「response_delivered」)private boolean responseDelivered;',但它在一個超類上,稱爲Transaction。 –

+0

我剛剛嘗試過'CAST上的org.app.model.Transaction.responseDelivered',但得到了同樣的錯誤。 –

+0

請顯示您的SignatureTransaction類 –