jpa Specification复杂查询

public  List<Receipts> test(List<String> costIds){
        Specification<Receipts> specification1= (Root<Receipts> root, CriteriaQuery<?> query, CriteriaBuilder cb)-> {
            Predicate restrictions = cb.conjunction();
//            List<Predicate> restrictions = new ArrayList<Predicate>();
            if (null != costIds) {
                Join<Receipts, ReceiptsCost> join = root.join("addReceiptsCostVOList",JoinType.LEFT);
//                Fetch<Receipts, ReceiptsCost> fetch=root.fetch("addReceiptsCostVOList",JoinType.LEFT);
//                restrictions = cb.and(restrictions,root.get("addReceiptsCostVOList").get("costId").in(costIds));
//                get("costchanges").get("costchangeApplyId")
//                root.fetch("addReceiptsCostVOList");
                restrictions=cb.and(join.get("costId").in(costIds));
            }
//            Predicate[] p = new Predicate[restrictions.size()];
//            return cb.and(restrictions.toArray(p));
            query.where(restrictions);
            query.distinct(true);
            return query.getRestriction();
        };
//        List<Receipts> receiptsList=receiptsRepository.findAll(specification1);
        return null;
    }
CriteriaQuery 可以添加一些字段之外的条件,比如去重 分组 等等

参考:https://blog.csdn.net/baijunzhijiang_01/article/details/51557125

原文地址:https://www.cnblogs.com/double-yuan/p/9072086.html