Java Oracle异常 - "列表中的最大表达式数为1000"

Ana*_*and 17 java oracle hibernate ora-01795

我将一个字符串列表传递给我的查询(SQL查询编写)以获取所需的数据.但是我得到了这个例外:

ora-01795列表中的最大表达式数为1000

我检查了传递给查询IN参数的列表中有超过1000个条目.

dig*_*ebs 14

这是查询中列表传递次数的oracle限制.

  1. 你将不得不砍掉你的查询或
  2. 改为在IN子句中提供子查询/连接.


小智 9

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.

如果您使用的是hibernate Criteria,则可以使用以下Java方法执行此操作.只需在您使用过的地方替换您的代码

criteria.add(Restrictions.in(propertyName, mainList));
Run Code Online (Sandbox Code Playgroud)

addCriteriaIn(propertyName, mainList, criteria);
Run Code Online (Sandbox Code Playgroud)

方法是:

 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);
  }
Run Code Online (Sandbox Code Playgroud)


Val*_*ade 5

我通过将列表分成大小为1000的批处理并使用OR连接它来解决这个问题.

例如eid []数组.

如果我想执行此查询,

String sql = select * from employee where some conditions and empid in(eid)
Run Code Online (Sandbox Code Playgroud)

我通过编写一小段代码重写了这个查询:

String sql = select * from employee where some conditions and ( 
                             empid in(empid[0...999]) OR
                             empid in(empid[1000...1999]) OR
                             empid in(empid[2000...2999]) OR .... );
Run Code Online (Sandbox Code Playgroud)

在使用休眠时处理此错误,您必须通过将列表分成100个批处理然后加入单个结果来解决此问题(如上面的查询中所示).

我不认为这是hibernate的限制因为没有处理这个问题,因为可能是这个问题不是像MySQL或DB2这样的另一个数据库的情况.Hibernate是一个跨DB的ORM框架.