From de4c356189ec3747124e9b4fd81dd37ce1d8a21d Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 19 Jul 2018 13:31:37 +0200 Subject: [PATCH] HHH-12827 Define a numeric type as decimal for DB2 DB2 converts the numeric type to decimal and returns Types.DECIMAL for a numeric. We change the type name used so that we have a match when comparing the type names as last resort. --- .../org/hibernate/dialect/DB2Dialect.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java index 7e3ea78b57..13a84cee85 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java @@ -40,6 +40,7 @@ import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.JdbcExceptionHelper; import org.hibernate.type.StandardBasicTypes; +import org.hibernate.type.descriptor.sql.DecimalTypeDescriptor; import org.hibernate.type.descriptor.sql.SmallIntTypeDescriptor; import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; @@ -100,7 +101,12 @@ public class DB2Dialect extends Dialect { registerColumnType( Types.TIME, "time" ); registerColumnType( Types.TIMESTAMP, "timestamp" ); registerColumnType( Types.VARBINARY, "varchar($l) for bit data" ); - registerColumnType( Types.NUMERIC, "numeric($p,$s)" ); + // DB2 converts numeric to decimal under the hood + // Note that the type returned by DB2 for a numeric column will be Types.DECIMAL. Thus, we have an issue when + // comparing the types during the schema validation, defining the type to decimal here as the type names will + // also be compared and there will be a match. See HHH-12827 for the details. + registerColumnType( Types.NUMERIC, "decimal($p,$s)" ); + registerColumnType( Types.DECIMAL, "decimal($p,$s)" ); registerColumnType( Types.BLOB, "blob($l)" ); registerColumnType( Types.CLOB, "clob($l)" ); registerColumnType( Types.LONGVARCHAR, "long varchar" ); @@ -210,7 +216,7 @@ public class DB2Dialect extends Dialect { registerKeyword( "only" ); getDefaultProperties().setProperty( Environment.STATEMENT_BATCH_SIZE, NO_BATCH ); - + uniqueDelegate = new DB2UniqueDelegate( this ); } @@ -365,7 +371,7 @@ public class DB2Dialect extends Dialect { @Override public ResultSet getResultSet(CallableStatement ps) throws SQLException { boolean isResultSet = ps.execute(); - // This assumes you will want to ignore any update counts + // This assumes you will want to ignore any update counts while ( !isResultSet && ps.getUpdateCount() != -1 ) { isResultSet = ps.getMoreResults(); } @@ -478,7 +484,14 @@ public class DB2Dialect extends Dialect { @Override protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) { - return sqlCode == Types.BOOLEAN ? SmallIntTypeDescriptor.INSTANCE : super.getSqlTypeDescriptorOverride( sqlCode ); + if ( sqlCode == Types.BOOLEAN ) { + return SmallIntTypeDescriptor.INSTANCE; + } + else if ( sqlCode == Types.NUMERIC ) { + return DecimalTypeDescriptor.INSTANCE; + } + + return super.getSqlTypeDescriptorOverride( sqlCode ); } @Override @@ -496,12 +509,12 @@ public class DB2Dialect extends Dialect { } }; } - + @Override public UniqueDelegate getUniqueDelegate() { return uniqueDelegate; } - + @Override public String getNotExpression( String expression ) { return "not (" + expression + ")";