spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

JdbcPagingItemReader - Not able to provide table alias in the column name in the sortKeys [BATCH-2854]

Open spring-projects-issues opened this issue 6 years ago • 5 comments

Shakti Tripathy opened BATCH-2854 and commented

If a table alias is given along with the column name for a sort key e.g. 

JdbcPagingItemReader reader = new JdbcPagingItemReader();

PostgresPagingQueryProvider provider = new PostgresPagingQueryProvider();

....

Map<String, Order> sortKeys = new HashMap<>();

sortKeys.put("mu.student_id", Order.ASCENDING);

provider.setSortKeys(sortKeys);

 

The code fails in JdbcPagingItemReader.PagingRowMapper 

private class PagingRowMapper implements RowMapper<T> {    @Override    public T mapRow(ResultSet rs, int rowNum) throws SQLException {       startAfterValues = new LinkedHashMap<>();       for (Map.Entry<String, Order> sortKey : queryProvider.getSortKeys().entrySet()) {         startAfterValues.put(sortKey.getKey(), rs.getObject(sortKey.getKey()));       }

     return rowMapper.mapRow(rs, rowNum);    } }

While its trying to get the column from resultset as mu.student_id, it fails. Can we remove any preceding aliases before the DOT(.) before pulling the column from the resultset.

 

The aliases are needed when using a join query.

 


Affects: 4.2.0

spring-projects-issues avatar Nov 05 '19 22:11 spring-projects-issues

I also encountered the same problem,Is this problem solved?

Scorpio-ljc avatar Mar 04 '21 01:03 Scorpio-ljc

@Scorpio-ljc I believe this is a duplicate of #1124 and #1208. Do you confirm?

Do you have a failing test or a minimal example that reproduces the issue?

fmbenhassine avatar Mar 12 '21 10:03 fmbenhassine

I had faced the similar issue and how i solved was to put column name as alias and use the same in the sort key. eg: table person has name , person_id and dob table address has address_id, person_id, place name

the query will be select a.person_id as "a.person_id" from Person a inner join Address b on a.person_id=b.person_id

and in the sort key , i provided "a.person_id"

deepak-kongath avatar Jun 22 '21 21:06 deepak-kongath

@deepak-kongath I just did what you said and it's sorted .. But the problem is the column "a.person_id" is not found in the resultSet ... it gives "null" ... is there a way to solve this please ?

carikk avatar Sep 06 '21 15:09 carikk

Hi @carikk - the answer by @deepak-kongath worked for me, I've documented it here - #1208.

himadripant avatar Dec 15 '21 02:12 himadripant