hibernate - 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 做到这一点吗?如果是,如何解决这个问题?
我为我糟糕的英语道歉。
解决方案
尝试这个:
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);
....
推荐阅读
- nan - 在生成 libffm.txt 文件作为字段分解机 (xLearn) 的输入时处理 NaN 和缺失值
- python - CPU 上的 tensorflow-mkl 问题
- java - 如何从键是java对象一部分的json字符串中提取特定值
- d - 将模板参数应用于函数而不调用函数?
- amazon-web-services - IAM 政策将一种类型的实例仅限制为 Ec2-classic
- html - 使用 selenium python 3 点击 web-table
- vba - 尝试运行删除查询时出现运行时错误 3008。错误是说它试图删除的表记录已经打开?
- laravel - View::composer 不在 boot() 服务提供者上工作
- c# - 在 linq 语句中重用选择新对象
- node.js - 从 Node.js 中 Azure Functions 代码中的外部 API 调用获取响应