Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JdbcTemplate.queryForObject(sql, <primitive>.class) produces TypeMismatchDataAccessException [SPR-13220] #17811

spring-projects-issues opened this issue Jul 10, 2015 · 1 comment


Copy link

@spring-projects-issues spring-projects-issues commented Jul 10, 2015

Rob Winch opened SPR-13220 and commented

The following code

int i = jdbcTemplate.queryForObject(sql,int.class);


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(
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(
	at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(
	at org.springframework.jdbc.core.JdbcTemplate.execute(
	at org.springframework.jdbc.core.JdbcTemplate.query(
	at org.springframework.jdbc.core.JdbcTemplate.query(
	at org.springframework.jdbc.core.JdbcTemplate.queryForObject(
	at org.springframework.jdbc.core.JdbcTemplate.queryForObject(

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:

int i = jdbcTemplate.queryForInt(sql);

to be

int i = jdbcTemplate.queryForObject(sql, int.class);

which would produce the above TypeMismatchDataAccessException. Obviously one could easily do:

int i = jdbcTemplate.queryForObject(sql, Integer.class);

to workaround the issue.

Affects: 4.2 RC2

Issue Links:

  • #17179 Remove pre-3.2 deprecated classes and methods

Referenced from: pull request #836, and commits e032930

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 13, 2015

Juergen Hoeller commented

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.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants