Browse Source

Explicit support for retrieving enum values

Issue: SPR-14990
pull/1264/head
Juergen Hoeller 8 years ago
parent
commit
278a6250e6
  1. 19
      spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java

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

@ -36,6 +36,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.CannotGetJdbcConnectionException; import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.NumberUtils;
/** /**
* Generic utility methods for working with JDBC. Mainly for internal use * Generic utility methods for working with JDBC. Mainly for internal use
@ -184,6 +185,24 @@ public abstract class JdbcUtils {
else if (Clob.class == requiredType) { else if (Clob.class == requiredType) {
return rs.getClob(index); return rs.getClob(index);
} }
else if (requiredType.isEnum()) {
// Enums can either be represented through a String or an enum index value:
// leave enum type conversion up to the caller (e.g. a ConversionService)
// but make sure that we return nothing other than a String or an Integer.
Object obj = rs.getObject(index);
if (obj instanceof String) {
return obj;
}
else if (obj instanceof Number) {
// Defensively convert any Number to an Integer (as needed by our
// ConversionService's IntegerToEnumConverterFactory) for use as index
return NumberUtils.convertNumberToTargetClass((Number) obj, Integer.class);
}
else {
// e.g. on Postgres: getObject returns a PGObject but we need a String
return rs.getString(index);
}
}
else { else {
// Some unknown type desired -> rely on getObject. // Some unknown type desired -> rely on getObject.

Loading…
Cancel
Save