From 02b45362dcb0a6df0cdb113e330d3c95450d5388 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sat, 15 Nov 2025 10:08:42 +0100 Subject: [PATCH] HHH-19749/HHH-18860 fix to precision of double/float mapped to DECIMAL/NUMERIC --- .../type/descriptor/java/DoubleJavaType.java | 20 ++++++------------- .../type/descriptor/java/FloatJavaType.java | 19 ++++++------------ 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoubleJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoubleJavaType.java index fff166f2d591..3bf60f6c2497 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoubleJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoubleJavaType.java @@ -15,9 +15,6 @@ import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; -import static java.lang.Math.ceil; -import static java.lang.Math.log; - /** * Descriptor for {@link Double} handling. * @@ -26,8 +23,6 @@ public class DoubleJavaType extends AbstractClassJavaType implements PrimitiveJavaType { public static final DoubleJavaType INSTANCE = new DoubleJavaType(); - //needed for converting precision from binary to decimal digits - private static final double LOG_BASE10OF2 = log(2)/log(10); public DoubleJavaType() { super( Double.class ); @@ -156,17 +151,14 @@ public long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) { @Override public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) { - if ( jdbcType.isFloat() ) { - //this is the number of *binary* digits - //in a double-precision FP number - return dialect.getDoublePrecision(); - } - else { - return Math.min( dialect.getDefaultDecimalPrecision(), (int) ceil( dialect.getDoublePrecision() * LOG_BASE10OF2 ) ); - } + return jdbcType.isFloat() + // this is usually the number of *binary* digits + // in a double-precision FP number + ? dialect.getDoublePrecision() + // this is the number of decimal digits in a Java double + : 17; } - @Override public Double coerce(X value, CoercionContext coercionContext) { if ( value == null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java index e65e1a24deb6..4a6c6e9e8f5d 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java @@ -15,9 +15,6 @@ import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; -import static java.lang.Math.ceil; -import static java.lang.Math.log; - /** * Descriptor for {@link Float} handling. * @@ -25,8 +22,6 @@ */ public class FloatJavaType extends AbstractClassJavaType implements PrimitiveJavaType { public static final FloatJavaType INSTANCE = new FloatJavaType(); - //needed for converting precision from binary to decimal digits - private static final double LOG_BASE10OF2 = log(2)/log(10); public FloatJavaType() { super( Float.class ); @@ -154,14 +149,12 @@ public long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) { @Override public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) { - if ( jdbcType.isFloat() ) { - //this is the number of *binary* digits - //in a single-precision FP number - return dialect.getFloatPrecision(); - } - else { - return Math.min( dialect.getDefaultDecimalPrecision(), (int) ceil( dialect.getFloatPrecision() * LOG_BASE10OF2 ) ); - } + return jdbcType.isFloat() + // this is usually the number of *binary* digits + // in a single-precision FP number + ? dialect.getFloatPrecision() + // this is the number of decimal digits in a Java float + : 8; } @Override