小智 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)
我通过将列表分成大小为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框架.
| 归档时间: |
|
| 查看次数: |
43748 次 |
| 最近记录: |