From 11c76129bd613b1fe551d87c234ae240a824e2d9 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 23 Jul 2015 10:28:43 -0500 Subject: [PATCH] HHH-9969 - MySQLDialect handling of numeric cast targets is incorrect --- .../org/hibernate/dialect/MySQLDialect.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java index d02c79e003..f0b882d687 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java @@ -30,6 +30,7 @@ import org.hibernate.hql.spi.id.local.AfterUseAction; import org.hibernate.hql.spi.id.local.LocalTemporaryTableBulkIdStrategy; import org.hibernate.internal.util.JdbcExceptionHelper; import org.hibernate.internal.util.StringHelper; +import org.hibernate.mapping.Column; import org.hibernate.type.StandardBasicTypes; /** @@ -326,11 +327,13 @@ public class MySQLDialect extends Dialect { case Types.INTEGER: case Types.BIGINT: case Types.SMALLINT: - return "signed"; + return smallIntegerCastTarget(); case Types.FLOAT: + case Types.REAL: { + return floatingPointNumberCastTarget(); + } case Types.NUMERIC: - case Types.REAL: - return "decimal"; + return fixedPointNumberCastTarget(); case Types.VARCHAR: return "char"; case Types.VARBINARY: @@ -340,6 +343,37 @@ public class MySQLDialect extends Dialect { } } + /** + * Determine the cast target for {@link Types#INTEGER}, {@link Types#BIGINT} and {@link Types#SMALLINT} + * + * @return The proper cast target type. + */ + protected String smallIntegerCastTarget() { + return "signed"; + } + + /** + * Determine the cast target for {@link Types#FLOAT} and {@link Types#REAL} (DOUBLE) + * + * @return The proper cast target type. + */ + protected String floatingPointNumberCastTarget() { + // MySQL does not allow casting to DOUBLE nor FLOAT, so we have to cast these as DECIMAL. + // MariaDB does allow casting to DOUBLE, although not FLOAT. + return fixedPointNumberCastTarget(); + } + + /** + * Determine the cast target for {@link Types#NUMERIC} + * + * @return The proper cast target type. + */ + protected String fixedPointNumberCastTarget() { + // NOTE : the precision/scale are somewhat arbitrary choices, but MySQL/MariaDB + // effectively require *some* values + return "decimal(" + Column.DEFAULT_PRECISION + "," + Column.DEFAULT_SCALE + ")"; + } + @Override public boolean supportsCurrentTimestampSelection() { return true;