I have a problem using RowMappers with JDBC (especially ParameterizedSingleColumnRowMapper) :
I am querying a list of existing ids from a database with the following :
List<Long> existingIds = DS.getJdbcTemplate().query(sql,
new ParameterizedSingleColumnRowMapper<Long>());
The only problem is that my list sometimes does not contains long as expected :
// for some value...
System.out.println(existingIds.get(0) instanceof Long); // return FALSE
System.out.println((Object)existingIds.get(0) instanceof Integer); // return TRUE
I could just go through existingIds and recast the values to long but I was expecting the row mapper to do that (I guess ParameterizedSingleColumnRowMapper is using getLong or something like that and usually it tries to cast to the desired value).
Do you have any explanation or ideas to solve that ?
Thanks in advance for your help.
You need to do
ParameterizedSingleColumnRowMapper.newInstance(Long.class). Just creating a new instance directly means it doesn’t know the type correctly (it can’t infer it from the generics because they get erased at compile time) so it probably just does.getObject()which will be at the mercy of the JDBC driver.