我有一个关于CriteriaBuilder API的问题。我是JPA的新手,我想创建(和或(组合
predicates.add(cb.equal(root.get("title"),title));
predicates.add(
cb.or(cb.between(root.get("startedAt"),startTime,endTime),
cb.between(root.get("endedAt"),startTime,endTime),
cb.and(
cb.greaterThanOrEqualTo(root.get("endedAt").as(Date.class),endTime),
cb.lessThanOrEqualTo(root.get("startedAt").as(Date.class),startTime))
));
输出结果
where title = ? and (started_at between ? and ? or started_at between ? and ? or
ended_at>=? and started_at <=?)
我想要的是一个结果
where title = ? and (started_at between ? and ? or started_at between ? and ? or
(ended_at>=? and started_at <=?)
)
如何使用CriteriaBuilder实现这一点?谢谢你的建议
你试过这个吗?
predicates.add(cb.and(cb.equal(root.get("title"),title),
cb.or(cb.or(cb.between(root.get("startedAt"),startTime,endTime),
cb.between(root.get("endedAt"),startTime,endTime)),
cb.and(cb.greaterThanOrEqualTo(root.get("endedAt").as(Date.class),endTime),
cb.lessThanOrEqualTo(root.get("startedAt").as(Date.class),startTime)))
));