SpringDataJpa for the ORACLE IN length is 1000

  ORACLE for SQLSELECT ... FROM A WHERE A.ID IN (SELECT ID FROM B) is not limited length,

In JPA IN length but more than 1,000 will be given for this case we can partition the contents of the IN, and then connected with OR,

Ado, directly on the code.

 public List<ComplaintsNotifyExportView> findByNotificationTyp(List<String> idList, NotificationType notificationType) {
        List<ComplaintsNotifyExportView> allList = complaintsNotifyExportViewDao.findAll(new Specification<ComplaintsNotifyExportView>() {
            @Override
            public Predicate toPredicate(Root<ComplaintsNotifyExportView> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                List<Predicate> listOr = new ArrayList<Predicate>();
                Predicate [] predicateOr = null;
                if (CollectionUtils.isNotEmpty(idList)) {
                    Path<String> path = root.get("complaintsId");
                    if (idList.size() > MAX_LENGTH) {
                        List<List<String>> lists = segmentationList(idList, MAX_LENGTH-1);
                        for (List<String> list : lists) {
                            CriteriaBuilder.In<String> in = cb.in(path);
                            for (int i = 0; i < list.size(); i++) {
                                in.value(list.get(i));
                            }
                            listOr.add(in);
                        }
                        predicateOr = new Predicate[listOr.size()];
                    } else {
                        CriteriaBuilder.In<String> in = cb.in(path);
                        for (int i = 0; i <idList.size() ; i++) {
                            in.value(idList.get(i));
                        }
                        predicates.add(in);
                    }
                }
                if (null != notificationType) {
                    predicates.add(cb.equal(root.get("notificationType").as(NotificationType.class), notificationType));
                }
                Predicate [] predicateAnd = new Predicate[predicates.size()];
                if (predicateOr != null) {
                    return query.where(cb.and(predicates.toArray(predicateAnd)), cb.or(listOr.toArray(predicateOr))).getRestriction();
                } else {
                    return query.where(cb.and(predicates.toArray(predicateAnd))).getRestriction();
                }
            }
        });
        return allList;
    }


 public static List<List<String>> segmentationList(List<String> targe, int size) {
        List<List <String >> = listArrnewThe ArrayList <List <>> String ();
         // Get the number of array is split 
        int arrSize = targe.size () ==% size 0 targe.size () /? Size: Targe 
                .size () / size + . 1 ;
         for ( int I = 0 ; I <arrSize; I ++ ) { 
            List <String> = Sub new new the ArrayList <String> ();
             // the specified index into the list of data 
            for ( int J = I * size; J <* size = (I + . 1 ) - . 1 ; J ++ ) {
                 IF (j <= targe.size() - 1) {
                    sub.add(targe.get(j));
                }
            }
            listArr.add(sub);
        }
        return listArr;
    }

 

 

Guess you like

Origin www.cnblogs.com/sly1994/p/11821915.html