我如何将 spring JPA 规范用于 where 条件如下:
其中 cond1 和(cond2 或 cond3)
和
其中(cond1 和 cond2)或 cond3 .
规范代码如下:
Specification<DocRecord> updatedAtAndCountGTZeroSpec = new Specification<DocRecord>() {
@Override
public Predicate toPredicate(Root<DocRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.or(
(cb.and(
cb.lessThanOrEqualTo(root.get("updatedAt"),new Timestamp(now.getTimeInMillis())),
cb.lessThan(root.get("attemptCount"), 3))
),
(cb.equal(root.get("attemptCount"), 0))
);
}
};
以上是生产条件如下:
哪里 docrecord0_.updated_at < ?
和 docrecord0_.attempt_count<3
或 docrecord0_.attempt_count=0
最佳答案
您可以构建“OR”规范,然后为第一个规范添加“AND”规范
Specification<?> spec = spec1.or(spec2);
spec = spec.and(spec3);
spec is actually equal to (spec1 OR spec2) AND spec3
关于java - Spring JPA 规范 Where 条件括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55180004/