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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM