jpa Specification复杂查询

public  List test(List costIds){
        Specification specification1= (Root root, CriteriaQuery query, CriteriaBuilder cb)-> {
            Predicate restrictions = cb.conjunction();
//            List restrictions = new ArrayList();
            if (null != costIds) {
                Join join = root.join("addReceiptsCostVOList",JoinType.LEFT);
//                Fetch 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 receiptsList=receiptsRepository.findAll(specification1);
        return null;
    }
CriteriaQuery 可以添加一些字段之外的条件,比如去重 分组 等等

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

你可能感兴趣的:(jpa Specification复杂查询)