From 278a6250e6643eaa856c62faf047b33201a7ab99 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 8 Dec 2016 18:30:25 +0100 Subject: [PATCH] Explicit support for retrieving enum values Issue: SPR-14990 --- .../jdbc/support/JdbcUtils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java index a8ffad62c6..ef7c972ed8 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java +++ b/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.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.