首页 > 解决方案 > JPA Criteria API selectCase in where 子句

问题描述

我尝试使用 JPA Criteria API (Hibernate) 将 selectCase 放入 where 子句

像这样的东西:

where
prerequis3_.rank_ID=2
and case when aptitudest5_.state=true 
then (
            aptitudest5_.aptitude_ID in (3 , 4 , 1 , 6 , 2 , 8 , 7)
        ) 
else (
            aptitudest5_.aptitude_ID not in  (3 , 4 , 1 , 6 , 2 , 8 , 7)
        ) 
end

我为 Criteria Api 尝试了许多不同的方法,如下所示:

Predicate prank = cb.equal(prerequis.get("rank"), u.getRank());
In<Long> inClause = cb.in(astate.get("aptitude").get("id"));        
for (Aptitude a : u.getV().getAptitudes()) {
    inClause.value(a.getId());
}
SimpleCase<Boolean, Boolean> caseExpression = cb.selectCase(astate.get("state"));
    caseExpression.when( true,cb.isTrue(inClause));
    caseExpression .otherwise(cb.isTrue(inClause.not()));
Predicate finalPr = cb.and(prank, caseExpression);
cq.where(finalPr);

但总是有同样的错误

19:04:19.242 [http-nio-8080-exec-132] ERROR org.hibernate.hql.internal.ast.ErrorTracker - line 1:391: unexpected token: in
19:04:19.242 [http-nio-8080-exec-132] ERROR org.hibernate.hql.internal.ast.ErrorTracker - line 1:391: unexpected token: in
antlr.NoViableAltException: unexpected token: in

我的问题是:可以用 Criteria API 做到这一点吗?如果是,如何解决这个问题?

我为我糟糕的英语道歉。

标签: hibernatejpacriteria-apicase-when

解决方案


尝试这个:

Predicate prank = cb.equal(prerequis.get("rank"), u.getRank());

//Get a list of ids
List<Integer> idsList = u.getV().getAptitudes().stream().map(Aptitude::getId)
                            .collect(Collectors.toList());
//Use in method from expression not from criteria builder
astate.get("aptitude").get("id").in(idsList);

....

推荐阅读