spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.6k stars 38.13k forks source link

Support enums when using JdbcTemplate with SqlParameterSource #26904

Closed belovaf closed 1 year ago

belovaf commented 3 years ago

JdbcTemplate delegates parameters binding to StatementCreatorUtils, which uses a fallback if sqlType and java type are unknown: https://github.com/spring-projects/spring-framework/blob/582b94d50ec880d06fecd506305c412c2a89b933/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java#L414

Can we handle enum values as Strings here? It will be consistent in my opinion, because other classes like BeanPropertyRowMapper support enums.

It also can be handled in SqlParameterSource implementations if getSqlType will return Types.VARCHAR for enums.

snicoll commented 1 year ago

I don't think that's a good idea to convert the input value in such a case. Also, it's asymmetrical as you can't get back the value if it's set this way. Thanks for the suggestion, in any case.