spring-batch
spring-batch copied to clipboard
JdbcPagingItemReader - Not able to provide table alias in the column name in the sortKeys [BATCH-2854]
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
I also encountered the same problem,Is this problem solved?
@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?
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 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 ?
Hi @carikk - the answer by @deepak-kongath worked for me, I've documented it here - #1208.