You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
inti = jdbcTemplate.queryForObject(sql,int.class);
produces
org.springframework.dao.TypeMismatchDataAccessException: Type mismatch affecting row number 0 and column type 'null': Value [22] is of type [java.lang.Integer] and cannot be converted to required type [int]
at org.springframework.jdbc.core.SingleColumnRowMapper.mapRow(SingleColumnRowMapper.java:101)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:459)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:404)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:470)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:480)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:490)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:496)
Instead, Spring should convert the Integer to an int.
Migrating from Spring 4.1 to 4.2
This bug is much more apparent now that Spring 4.2 removed methods like int queryForInt(String sql). It seems that many users may update the following Spring pre 4.2 code:
inti = jdbcTemplate.queryForInt(sql);
to be
inti = jdbcTemplate.queryForObject(sql, int.class);
which would produce the above TypeMismatchDataAccessException. Obviously one could easily do:
inti = jdbcTemplate.queryForObject(sql, Integer.class);
Fixed through a ClassUtils.resolvePrimitiveIfNecessary call in SingleColumnRowMapper.setRequiredType now, not involving NumberUtils itself. This avoids any kind of unchecked casts or other deviations from the generically specified Number parameter type in the actual conversion algorithm.
Uh oh!
There was an error while loading. Please reload this page.
Rob Winch opened SPR-13220 and commented
The following code
produces
Instead, Spring should convert the Integer to an int.
Migrating from Spring 4.1 to 4.2
This bug is much more apparent now that Spring 4.2 removed methods like
int queryForInt(String sql)
. It seems that many users may update the following Spring pre 4.2 code:to be
which would produce the above
TypeMismatchDataAccessException
. Obviously one could easily do:to workaround the issue.
Affects: 4.2 RC2
Issue Links:
Referenced from: pull request #836, and commits e032930
The text was updated successfully, but these errors were encountered: