Skip to content

Add support for JdbcTempalte.queryForObject(sql,<primitive>.class) #836

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

Closed
wants to merge 1 commit into from

Conversation

rwinch
Copy link
Member

@rwinch rwinch commented Jul 10, 2015

Previously using jdbcTemplate.queryForObject(sql, int.class) produced
a TypeMismatchDataAccessException stating it could not convert an Integer
to an int.

This update allows conversion to the respective primitives instead of just
the wrapper Objects.

Issue: SPR-13220

Previously using jdbcTemplate.queryForObject(sql, int.class) produced
a TypeMismatchDataAccessException stating it could not convert an Integer
to an int.

This update allows conversion to the respective primitives instead of just
the wrapper Objects.

Issue: SPR-13220
@panchenko
Copy link
Contributor

Wouldn't it be simpler to convert primitive class to the corresponding wrapper initially (when initializing SingleColumnRowMapper#requiredType) ?

@rwinch
Copy link
Member Author

rwinch commented Jul 11, 2015

@panchenko Thanks for your feedback. If I understand the question correctly, you wonder why we don't just convert the value in the constructor and / or setter? If so

  • NumberUtils.convertNumberToTargetClass(Number, Class<T>) is used in multiple places (and may be used in more places in the future), so I think that is the correct place to fix this.
  • convertValueToRequiredType(Object,Class<?>) which should work for primitive values as well For example, while mapRow passes in this.requiredType, the class SingleColumnRowMapper is non final so anyone that extends the class can pass in a different type to the method.
  • As far as I know there is no easy way to convert from the primitive class to its corresponding wrapper class, so this actually involves quite a bit of code.

Does this help? If not, can you please clarify? Perhaps I am not understanding your proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants