Explicit support for retrieving enum values
Issue: SPR-14990
This commit is contained in:
parent
ee30ce95fe
commit
278a6250e6
|
@ -36,6 +36,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
|
||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||
import org.springframework.jdbc.datasource.DataSourceUtils;
|
||||
import org.springframework.util.NumberUtils;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with JDBC. Mainly for internal use
|
||||
|
@ -184,6 +185,24 @@ public abstract class JdbcUtils {
|
|||
else if (Clob.class == requiredType) {
|
||||
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 {
|
||||
// Some unknown type desired -> rely on getObject.
|
||||
|
|
Loading…
Reference in New Issue