From 75e1f17139db3327a2d141d0d2b4c628ff990bac Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 3 May 2024 14:43:40 +0200 Subject: [PATCH] HHH-18065 Truncate date related milliseconds from value passed to java.sql.Time --- .../descriptor/java/CalendarDateJavaType.java | 2 +- .../type/descriptor/java/CalendarJavaType.java | 2 +- .../descriptor/java/CalendarTimeJavaType.java | 2 +- .../type/descriptor/java/DateJavaType.java | 2 +- .../type/descriptor/java/InstantJavaType.java | 2 +- .../type/descriptor/java/JdbcTimeJavaType.java | 18 +++++++++++------- .../descriptor/java/JdbcTimestampJavaType.java | 2 +- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarDateJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarDateJavaType.java index fb5c8020d3..64f19ab1c1 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarDateJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarDateJavaType.java @@ -102,7 +102,7 @@ public class CalendarDateJavaType extends AbstractTemporalJavaType { return (X) new java.sql.Date( value.getTimeInMillis() ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { - return (X) new java.sql.Time( value.getTimeInMillis() ); + return (X) new java.sql.Time( value.getTimeInMillis() % 86_400_000 ); } if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) { return (X) new java.sql.Timestamp( value.getTimeInMillis() ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarJavaType.java index af3c986d75..c7f0b56789 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarJavaType.java @@ -119,7 +119,7 @@ public class CalendarJavaType extends AbstractTemporalJavaType impleme return (X) new java.sql.Date( value.getTimeInMillis() ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { - return (X) new java.sql.Time( value.getTimeInMillis() ); + return (X) new java.sql.Time( value.getTimeInMillis() % 86_400_000 ); } if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) { return (X) new java.sql.Timestamp( value.getTimeInMillis() ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarTimeJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarTimeJavaType.java index c9ccfca1df..79b2226bc0 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarTimeJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarTimeJavaType.java @@ -104,7 +104,7 @@ public class CalendarTimeJavaType extends AbstractTemporalJavaType { return (X) new java.sql.Date( value.getTimeInMillis() ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { - return (X) new java.sql.Time( value.getTimeInMillis() ); + return (X) new java.sql.Time( value.getTimeInMillis() % 86_400_000 ); } if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) { return (X) new java.sql.Timestamp( value.getTimeInMillis() ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DateJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DateJavaType.java index 72a21df212..36bb6e12df 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DateJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DateJavaType.java @@ -125,7 +125,7 @@ public class DateJavaType extends AbstractTemporalJavaType implements Vers if ( java.sql.Time.class.isAssignableFrom( type ) ) { final java.sql.Time rtn = value instanceof java.sql.Time ? ( java.sql.Time ) value - : new java.sql.Time( value.getTime() ); + : new java.sql.Time( value.getTime() % 86_400_000 ); return (X) rtn; } if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java index c34af011a4..f4842fcc3b 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/InstantJavaType.java @@ -123,7 +123,7 @@ public class InstantJavaType extends AbstractTemporalJavaType } if ( java.sql.Time.class.isAssignableFrom( type ) ) { - return (X) new java.sql.Time( instant.toEpochMilli() ); + return (X) new java.sql.Time( instant.toEpochMilli() % 86_400_000 ); } if ( Date.class.isAssignableFrom( type ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java index f83a1845b3..be6a0115d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimeJavaType.java @@ -131,7 +131,7 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { if ( LocalTime.class.isAssignableFrom( type ) ) { final Time time = value instanceof java.sql.Time ? ( (java.sql.Time) value ) - : new java.sql.Time( value.getTime() ); + : new java.sql.Time( value.getTime() % 86_400_000 ); final LocalTime localTime = time.toLocalTime(); final long millis = time.getTime() % 1000; if ( millis == 0 ) { @@ -143,7 +143,7 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { if ( Time.class.isAssignableFrom( type ) ) { return value instanceof Time ? value - : new Time( value.getTime() ); + : new Time( value.getTime() % 86_400_000 ); } if ( Date.class.isAssignableFrom( type ) ) { @@ -181,6 +181,14 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { return null; } + if ( value instanceof Time ) { + return (Date) value; + } + + if ( value instanceof Date ) { + return new Time( ( (Date) value ).getTime() % 86_400_000 ); + } + if ( value instanceof LocalTime ) { final LocalTime localTime = (LocalTime) value; final Time time = Time.valueOf( localTime ); @@ -191,16 +199,12 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType { return new Time( time.getTime() + DateTimeUtils.roundToPrecision( localTime.getNano(), 3 ) / 1000000 ); } - if ( value instanceof Date ) { - return (Date) value; - } - if ( value instanceof Long ) { return new Time( (Long) value ); } if ( value instanceof Calendar ) { - return new Time( ( (Calendar) value ).getTimeInMillis() ); + return new Time( ( (Calendar) value ).getTimeInMillis() % 86_400_000 ); } throw unknownWrap( value.getClass() ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java index e0fda9d90e..fbee5d86bd 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcTimestampJavaType.java @@ -158,7 +158,7 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType implem if ( java.sql.Time.class.isAssignableFrom( type ) ) { return value instanceof java.sql.Time ? ( java.sql.Time ) value - : new java.sql.Time( value.getTime() ); + : new java.sql.Time( value.getTime() % 86_400_000 ); } throw unknownUnwrap( type );