Skip to content

Commit 278a625

Browse files
committed
Explicit support for retrieving enum values
Issue: SPR-14990
1 parent ee30ce9 commit 278a625

File tree

1 file changed

+19
-0
lines changed
  • spring-jdbc/src/main/java/org/springframework/jdbc/support

1 file changed

+19
-0
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import org.springframework.jdbc.CannotGetJdbcConnectionException;
3838
import org.springframework.jdbc.datasource.DataSourceUtils;
39+
import org.springframework.util.NumberUtils;
3940

4041
/**
4142
* Generic utility methods for working with JDBC. Mainly for internal use
@@ -184,6 +185,24 @@ else if (Blob.class == requiredType) {
184185
else if (Clob.class == requiredType) {
185186
return rs.getClob(index);
186187
}
188+
else if (requiredType.isEnum()) {
189+
// Enums can either be represented through a String or an enum index value:
190+
// leave enum type conversion up to the caller (e.g. a ConversionService)
191+
// but make sure that we return nothing other than a String or an Integer.
192+
Object obj = rs.getObject(index);
193+
if (obj instanceof String) {
194+
return obj;
195+
}
196+
else if (obj instanceof Number) {
197+
// Defensively convert any Number to an Integer (as needed by our
198+
// ConversionService's IntegerToEnumConverterFactory) for use as index
199+
return NumberUtils.convertNumberToTargetClass((Number) obj, Integer.class);
200+
}
201+
else {
202+
// e.g. on Postgres: getObject returns a PGObject but we need a String
203+
return rs.getString(index);
204+
}
205+
}
187206

188207
else {
189208
// Some unknown type desired -> rely on getObject.

0 commit comments

Comments
 (0)