[英]How to use CASE WHEN in Hibernate Where clause
Plase give example for using CASE WHEN
in HQL. Plase给出了在HQL中使用
CASE WHEN
示例。
I have used following query in my code. 我在我的代码中使用了以下查询。
int receiptNumber = 100;
String hql = "SELECT b FROM OOPExtract as b "
+"WHERE "
+" b.tranStatId =" +receiptNumber+ " AND "
+" b.orderType IN ('EMERGENCY', 'PLENARY', 'PETITION','EXTENSION','MOTION') AND "
+" CASE WHEN b.orderType == 'MOTION' " `enter code here`
+ "THEN "
+" b.status = 'MOTION_SIGNED' "
+" ELSE "
+" b.status LIKE '%%' "
+" END "
+" ORDER BY b.oopExtractId DESC";
But when it run it's generate following exception 但是当它运行时它会产生以下异常
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: = .....
org.hibernate.hql.ast.QuerySyntaxException:意外令牌:= .....
I've used the following query in a working project. 我在一个工作项目中使用了以下查询。 Could use it as a template :)
可以用它作为模板:)
"SELECT "
+ "CASE WHEN smth.status != 'BATMAN' THEN ... "
+ "ELSE (SELECT ... FROM ... WHERE ... ORDER BY ... DESC limit 1) "
+ "END, "
+ "next_field, "
+ "CASE WHEN smth.status == 'BATMAN' THEN ... "
+ "ELSE ... "
+ "END, "
+ "final_field_which_doesent_have_a_case_logic"
+ "FROM ... the_rest_of_the_normal_query";
This one works with Hibernate 4/JPA and Spring 4 as well! 这个也适用于Hibernate 4 / JPA和Spring 4!
select
limite
from
Limite limite
join
fetch limite.limiteEnqs enq
join
fetch enq.limiteValors valor
where
limite.id = :limiteId
and :dataReferencia between valor.dtIniVig and valor.dtFimVig
and enq.vrAtributo1 = case limite.atributoId1
when 8 then :produtoId
else enq.vrAtributo1
end
Please try this. 请试试这个。 Hope it Work.
希望它有效。
"SELECT b FROM OOPExtract as b WHERE "
+" b.tranStatId =" +receiptNumber+ " AND "
+" b.orderType IN ('EMERGENCY', 'PLENARY', 'PETITION','EXTENSION','MOTION') AND "
**+" CASE WHEN b.orderType 'MOTION' " `enter code here`
+ "THEN 'MOTION_SIGNED' "
+" ELSE "**
+" b.status LIKE '%%' "
+" END "
+" ORDER BY b.oopExtractId DESC";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.