一、criteriaBuilder.in使用
需求:在原有的Jpa查询上过滤订单状态。类似于Sql: where status in (1,2,3)
参考:https://stackoverflow.com/questions/9321916/jpa-criteriabuilder-how-to-use-in-comparison-operator
方法一:
Path<Object> path = root.get("status"); CriteriaBuilder.In<Object> in = criteriaBuilder.in(path); in.value(OrderRefund.Status.pending); in.value(OrderRefund.Status.approved); in.value(OrderRefund.Status.received); in.value(OrderRefund.Status.receiveing); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.and(in));
方法二:
Path<Object> path = root.get("hospId"); CriteriaBuilder.In<Object> in = criteriaBuilder.in(path); for (BigInteger hospId:hospIds){ in.value(hospId); } predicates.add(criteriaBuilder.and(in));
方法二更值得推荐。