首页 > 解决方案 > How to pass a very long list in jdbcTemplate spring-boot

问题描述

I am using oracle datasource which does not allow more than 1000 records in a query.

My idList contain 10K or more which would be dynamic

My unit test is

@Test
    public void testLongListParameter(){
        String QUERY = "select value from sample_table where id in (:ids)"; 
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
        Map idMap = Collections.singletonMap("ids", idList);
        namedParameterJdbcTemplate.query(QUERY, idMap, new CustomRowMapper());

    }

when I run this I get ORA-01795: maximum number of expressions in a list is 1000

so the problem is I have more argument than limit of oracle. to resolve this do I have to write my custom query generator or does spring already have something similar.

标签: oraclespring-bootspring-jdbcjdbctemplate

解决方案


一切最终都必须使用 JDBC API……而且列表的限制100SELECT任何比这更大的东西都不能保证得到数据库的支持。您已经达到了 Oracle 的极限,但它也可能是其他一些供应商。

在这方面你真的无能为力。

在另一个领域,破解解决方案,您可以:

  1. IN子句分成几个OR'ed 块(块大小由您决定,您可以使用100or1000因为您知道这是您达到的限制)
  2. IN将数据集插入到临时表中,然后在子句中使用子查询

推荐阅读