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/51557125fetch