Skip to content

Commit 4e41f74

Browse files
committed
Explicit support for retrieving enum values
Issue: SPR-14990 (cherry picked from commit 278a625)
1 parent c6663f5 commit 4e41f74

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
@@ -38,6 +38,7 @@
3838
import org.springframework.jdbc.datasource.DataSourceUtils;
3939
import org.springframework.lang.UsesJava7;
4040
import org.springframework.util.ClassUtils;
41+
import org.springframework.util.NumberUtils;
4142

4243
/**
4344
* Generic utility methods for working with JDBC. Mainly for internal use
@@ -192,6 +193,24 @@ else if (Blob.class == requiredType) {
192193
else if (Clob.class == requiredType) {
193194
return rs.getClob(index);
194195
}
196+
else if (requiredType.isEnum()) {
197+
// Enums can either be represented through a String or an enum index value:
198+
// leave enum type conversion up to the caller (e.g. a ConversionService)
199+
// but make sure that we return nothing other than a String or an Integer.
200+
Object obj = rs.getObject(index);
201+
if (obj instanceof String) {
202+
return obj;
203+
}
204+
else if (obj instanceof Number) {
205+
// Defensively convert any Number to an Integer (as needed by our
206+
// ConversionService's IntegerToEnumConverterFactory) for use as index
207+
return NumberUtils.convertNumberToTargetClass((Number) obj, Integer.class);
208+
}
209+
else {
210+
// e.g. on Postgres: getObject returns a PGObject but we need a String
211+
return rs.getString(index);
212+
}
213+
}
195214

196215
else {
197216
// Some unknown type desired -> rely on getObject.

0 commit comments

Comments
 (0)