Search code examples
javaspring-batch

Spring Batch: How to configure PagingQueryProvider without a SortKeys


I'm trying to configure a JdbcPagingItemReader bean and I don't know exactly the names of the columns in the table so I'd like to know if there is a way to configure a PagingQueryProvider without a sort key.

@Bean
public JdbcPagingItemReader<Map<String, Object>> pagingItemReader(DataSource dataSource){
    JdbcPagingItemReader<Map<String, Object>> itemReader = new JdbcPagingItemReader<>();
    itemReader.setDataSource(dataSource);
    itemReader.setQueryProvider(queryProvider());
    itemReader.setPageSize(1000);

    itemReader.setRowMapper(BatchConfiguration::rowMapper);

    return itemReader;
}


/* This configuration throws the exception
 * 'java.lang.IllegalArgumentException: sortKey must be specified'
 */
private MySqlPagingQueryProvider queryProvider(){
    MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
    queryProvider.setSelectClause("SELECT *");
    queryProvider.setFromClause("FROM "+ clientTableName);

    return queryProvider;
}

Solution

  • I'd like to know if there is a way to configure a PagingQueryProvider without a sort key.

    No, this is not possible. The sortKey is mandatory to build the SQL statement. Here is an excerpt from the reference documentation:

    The SqlPagingQueryProviderFactoryBean requires that you specify a select clause and a from clause. You can also provide an optional where clause. These clauses and the required sortKey are used to build an SQL statement.