超全模态模型 × Harness 升级,升级解锁 ArkClaw,最新支持 DeepSeek-V4 系列与 GLM-5.1
我有一张名为workorder的表,包含id、shift、date、sop_no、type字段。需求是:用户通过GET API传入整数参数过滤表数据,若记录type='P'则用shift字段匹配参数,否则用sop_no字段匹配参数。
workorder
id
shift
date
sop_no
type
type='P'
尝试用JPA Specifications的selectCase()选择对应谓词时,代码抛出「Expression cannot be converted to Predicate」错误,使用as()转换类型也无效。原代码如下:workorderRepo.findAll(Specification.where(hasShift(Integer a)) public Specification<WorkOrder> hasShift(Integer a) { return ((root, query, criteriaBuilder) -> { Predicate nonP = criteriaBuilder.equal(root.get("sop_no"),a); Predicate P = criteriaBuilder.equal(root.get("shift"),a); return criteriaBuilder.selectCase().when(criteriaBuilder.equal(root.get("type"), "P"), P).otherwise(nonP); }); } 问题原因 #criteriaBuilder.selectCase()的设计目标是生成表达式(Expression),用于选择特定值或字段,而非返回用于数据过滤的Predicate对象,因此直接用它返回谓词会触发类型转换错误。解决方案 #通过构造两个分支条件并使用逻辑或(OR)组合,实现需求逻辑:分支1:type='P' 且 shift等于传入参数分支2:type!='P' 且 sop_no等于传入参数修正后的代码:public Specification<WorkOrder> hasFilter(Integer a) { return (root, query, criteriaBuilder) -> { // 构造type=P的基础条件 Predicate isTypeP = criteriaBuilder.equal(root.get("type"), "P"); // 分支1:type=P时匹配shift Predicate pBranch = criteriaBuilder.and(isTypeP, criteriaBuilder.equal(root.get("shift"), a)); // 分支2:type≠P时匹配sop_no Predicate nonPBranch = criteriaBuilder.and(criteriaBuilder.not(isTypeP), criteriaBuilder.equal(root.get("sop_no"), a)); // 组合两个分支,满足任意一个条件即可 return criteriaBuilder.or(pBranch, nonPBranch); }; } 调用示例:workorderRepo.findAll(Specification.where(hasFilter(a))); 内容的提问来源于stack exchange,提问作者Yugandhar Reddy Akkisetty
selectCase()
as()
workorderRepo.findAll(Specification.where(hasShift(Integer a))
public Specification<WorkOrder> hasShift(Integer a) { return ((root, query, criteriaBuilder) -> { Predicate nonP = criteriaBuilder.equal(root.get("sop_no"),a); Predicate P = criteriaBuilder.equal(root.get("shift"),a); return criteriaBuilder.selectCase().when(criteriaBuilder.equal(root.get("type"), "P"), P).otherwise(nonP); }); }
criteriaBuilder.selectCase()的设计目标是生成表达式(Expression),用于选择特定值或字段,而非返回用于数据过滤的Predicate对象,因此直接用它返回谓词会触发类型转换错误。
criteriaBuilder.selectCase()
Predicate
通过构造两个分支条件并使用逻辑或(OR)组合,实现需求逻辑:
type!='P'
修正后的代码:
public Specification<WorkOrder> hasFilter(Integer a) { return (root, query, criteriaBuilder) -> { // 构造type=P的基础条件 Predicate isTypeP = criteriaBuilder.equal(root.get("type"), "P"); // 分支1:type=P时匹配shift Predicate pBranch = criteriaBuilder.and(isTypeP, criteriaBuilder.equal(root.get("shift"), a)); // 分支2:type≠P时匹配sop_no Predicate nonPBranch = criteriaBuilder.and(criteriaBuilder.not(isTypeP), criteriaBuilder.equal(root.get("sop_no"), a)); // 组合两个分支,满足任意一个条件即可 return criteriaBuilder.or(pBranch, nonPBranch); }; }
调用示例:
workorderRepo.findAll(Specification.where(hasFilter(a)));
内容的提问来源于stack exchange,提问作者Yugandhar Reddy Akkisetty
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起
模型再升级,30秒超长叙事, 模态参考扩容
模型自由,工具不限,最新支持 Deepseek-V4 系列、GLM-5.3 系列
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列
大模型19元起,Al应用9.9元畅享,新人首购爆款尽享优惠