【发布时间】:2020-12-17 08:41:24
【问题描述】:
我尝试使用 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);
但是a总是有同样的错误
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 来做到这一点吗? 如果是,如何解决这个问题?
我很抱歉我的英语不好。
【问题讨论】:
标签: hibernate jpa criteria-api case-when