Java Oracle exception - “maximum number of expressions in a list is 1000”

后端 未结 7 724
再見小時候
再見小時候 2020-12-03 05:05

I am passing a list of Strings to my query(SQL query written) to fetch the required data. But I am getting this exception:

ora-01795 maximum number

7条回答
  •  盖世英雄少女心
    2020-12-03 05:52

    You cant have a list with more than 1000 elements in a single "where" condition if you are working with Oracle DB. So you can chop down your "where" condition in multiple "where" conditions and join them with "or" clause.

    If you are using hibernate Criteria, you can use below Java method to do this. Just replace your code where ever you used

    criteria.add(Restrictions.in(propertyName, mainList));
    

    with

    addCriteriaIn(propertyName, mainList, criteria);
    

    which the method is :

     private void addCriteriaIn (String propertyName, List list,Criteria criteria)
      {
        Disjunction or = Restrictions.disjunction();
        if(list.size()>1000)
        {        
          while(list.size()>1000)
          {
            List subList = list.subList(0, 1000);
            or.add(Restrictions.in(propertyName, subList));
            list.subList(0, 1000).clear();
          }
        }
        or.add(Restrictions.in(propertyName, list));
        criteria.add(or);
      }
    

提交回复
热议问题