How can i use spring JPA Specification for where condition as below:
Where cond1 and (cond2 or cond3)
AND
Where (cond1 and cond2) or cond3.
Specification code is as below:
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))
);
}
};
Above is production where condition as below:
where docrecord0_.updated_at < ? and docrecord0_.attempt_count<3 or docrecord0_.attempt_count=0
cb.and(cond1, cb.or(cond2, cond3))
? Not sure I understand your issue... – Machicolationor
. So you start bycb.or
. In your first snippet, the two main conditions are separated by and, so start bycb.and
. I don't get what the difficulty is. – Succeed