Skip to content

Commit 11c7612

Browse files
committed
HHH-9969 - MySQLDialect handling of numeric cast targets is incorrect
1 parent 1f9b1c0 commit 11c7612

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.hibernate.hql.spi.id.local.LocalTemporaryTableBulkIdStrategy;
3131
import org.hibernate.internal.util.JdbcExceptionHelper;
3232
import org.hibernate.internal.util.StringHelper;
33+
import org.hibernate.mapping.Column;
3334
import org.hibernate.type.StandardBasicTypes;
3435

3536
/**
@@ -326,11 +327,13 @@ public String getCastTypeName(int code) {
326327
case Types.INTEGER:
327328
case Types.BIGINT:
328329
case Types.SMALLINT:
329-
return "signed";
330+
return smallIntegerCastTarget();
330331
case Types.FLOAT:
332+
case Types.REAL: {
333+
return floatingPointNumberCastTarget();
334+
}
331335
case Types.NUMERIC:
332-
case Types.REAL:
333-
return "decimal";
336+
return fixedPointNumberCastTarget();
334337
case Types.VARCHAR:
335338
return "char";
336339
case Types.VARBINARY:
@@ -340,6 +343,37 @@ public String getCastTypeName(int code) {
340343
}
341344
}
342345

346+
/**
347+
* Determine the cast target for {@link Types#INTEGER}, {@link Types#BIGINT} and {@link Types#SMALLINT}
348+
*
349+
* @return The proper cast target type.
350+
*/
351+
protected String smallIntegerCastTarget() {
352+
return "signed";
353+
}
354+
355+
/**
356+
* Determine the cast target for {@link Types#FLOAT} and {@link Types#REAL} (DOUBLE)
357+
*
358+
* @return The proper cast target type.
359+
*/
360+
protected String floatingPointNumberCastTarget() {
361+
// MySQL does not allow casting to DOUBLE nor FLOAT, so we have to cast these as DECIMAL.
362+
// MariaDB does allow casting to DOUBLE, although not FLOAT.
363+
return fixedPointNumberCastTarget();
364+
}
365+
366+
/**
367+
* Determine the cast target for {@link Types#NUMERIC}
368+
*
369+
* @return The proper cast target type.
370+
*/
371+
protected String fixedPointNumberCastTarget() {
372+
// NOTE : the precision/scale are somewhat arbitrary choices, but MySQL/MariaDB
373+
// effectively require *some* values
374+
return "decimal(" + Column.DEFAULT_PRECISION + "," + Column.DEFAULT_SCALE + ")";
375+
}
376+
343377
@Override
344378
public boolean supportsCurrentTimestampSelection() {
345379
return true;

0 commit comments

Comments
 (0)