Reputation: 390
Plase give example for using CASE WHEN
in HQL.
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: = .....
Upvotes: 3
Views: 10663
Reputation: 889
This one works with Hibernate 4/JPA and Spring 4 as well!
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
Upvotes: 2
Reputation: 21
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";
Upvotes: 0
Reputation: 10253
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";
Upvotes: 2