我有以下HQL查询。
@Query("SELECT IF ( job.doneDate IS NOT NULL ) THEN SUM( jobCost.amount ) FROM JobCost AS jobCost "
+ "LEFT JOIN jobCost.job AS job WHERE job.code = :#{#filter.jobCode} "
+ "AND jobCost.costType = :#{#filter.jobCostType} "
+ "AND jobCost.job.business.id = :#{#filter.searchBusiness} "
+ "AND jobCost.date >= :job.doneDate "
+ "AND jobCost.date <= :#{#filter.endDate} ELSE 0.00 END")
我在控制台上遇到了这个错误。
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: THEN near line 1, column 40 [SELECT IF ( job.doneDate IS NOT NULL ) THEN SUM( jobCost.amount ) FROM com.job.JobCost AS jobCost LEFT JOIN jobCost.job AS job WHERE job.code = :__$synthetic$__1 AND jobCost.costType = :__$synthetic$__2 AND jobCost.job.business.id = :__$synthetic$__3 AND jobCost.date >= :job.doneDate AND jobCost.date <= :__$synthetic$__4 ELSE 0.00 END]
我该如何纠正?
答案 0 :(得分:2)
@Query("SELECT CASE WHEN job.doneDate IS NOT NULL THEN SUM(jobCost.amount) ELSE 0.00 END FROM JobCost AS jobCost "
+ "LEFT JOIN jobCost.job AS job WHERE job.code = :#{#filter.jobCode} "
+ "AND jobCost.costType = :#{#filter.jobCostType} "
+ "AND jobCost.job.business.id = :#{#filter.searchBusiness} "
+ "AND jobCost.date >= :job.doneDate "
+ "AND jobCost.date <= :#{#filter.endDate}")
请注意:在WHERE条件下不接受ELSE,你可以通过CASE来处理这个问题,当时它不是那么......那么......结束。
请参阅:https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/queryhql.html