diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsInstantJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsInstantJdbcType.java index cecd578d2b0e..95568f14a451 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsInstantJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsInstantJdbcType.java @@ -84,14 +84,9 @@ protected void doBind( int index, WrapperOptions wrapperOptions) throws SQLException { final Instant instant = javaType.unwrap( value, Instant.class, wrapperOptions ); - try { - // supposed to be supported in JDBC 4.2 - st.setObject( index, instant, Types.TIMESTAMP_WITH_TIMEZONE ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - st.setTimestamp( index, Timestamp.from( instant ), UTC_CALENDAR ); - } + // some jdbc drivers may support java.time.Instant directly but + // this is the only standard support in the jdbc 4.2 specification + st.setTimestamp( index, Timestamp.from( instant ), UTC_CALENDAR ); } @Override @@ -102,14 +97,9 @@ protected void doBind( WrapperOptions wrapperOptions) throws SQLException { final Instant instant = javaType.unwrap( value, Instant.class, wrapperOptions ); - try { - // supposed to be supported in JDBC 4.2 - st.setObject( name, instant, Types.TIMESTAMP_WITH_TIMEZONE ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - st.setTimestamp( name, Timestamp.from( instant ), UTC_CALENDAR ); - } + // some jdbc drivers may support java.time.Instant directly but + // this is the only standard support in the jdbc 4.2 specification + st.setTimestamp( name, Timestamp.from( instant ), UTC_CALENDAR ); } }; } @@ -119,38 +109,23 @@ public ValueExtractor getExtractor(final JavaType javaType) { return new BasicExtractor<>( javaType, this ) { @Override protected X doExtract(ResultSet rs, int position, WrapperOptions wrapperOptions) throws SQLException { - try { - // supposed to be supported in JDBC 4.2 - return javaType.wrap( rs.getObject( position, Instant.class ), wrapperOptions ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - return javaType.wrap( rs.getTimestamp( position, UTC_CALENDAR ), wrapperOptions ); - } + // some jdbc drivers may support java.time.Instant directly but + // this is the only standard support in the jdbc 4.2 specification + return javaType.wrap( rs.getTimestamp( position, UTC_CALENDAR ), wrapperOptions ); } @Override protected X doExtract(CallableStatement statement, int position, WrapperOptions wrapperOptions) throws SQLException { - try { - // supposed to be supported in JDBC 4.2 - return javaType.wrap( statement.getObject( position, Instant.class ), wrapperOptions ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - return javaType.wrap( statement.getTimestamp( position, UTC_CALENDAR ), wrapperOptions ); - } + // some jdbc drivers may support java.time.Instant directly but + // this is the only standard support in the jdbc 4.2 specification + return javaType.wrap( statement.getTimestamp( position, UTC_CALENDAR ), wrapperOptions ); } @Override protected X doExtract(CallableStatement statement, String name, WrapperOptions wrapperOptions) throws SQLException { - try { - // supposed to be supported in JDBC 4.2 - return javaType.wrap( statement.getObject( name, Instant.class ), wrapperOptions ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - return javaType.wrap( statement.getTimestamp( name, UTC_CALENDAR ), wrapperOptions ); - } + // some jdbc drivers may support java.time.Instant directly but + // this is the only standard support in the jdbc 4.2 specification + return javaType.wrap( statement.getTimestamp( name, UTC_CALENDAR ), wrapperOptions ); } }; } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsOffsetDateTimeJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsOffsetDateTimeJdbcType.java index 134fecc5d551..01ca53a205f4 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsOffsetDateTimeJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsOffsetDateTimeJdbcType.java @@ -83,7 +83,6 @@ protected void doBind( int index, WrapperOptions wrapperOptions) throws SQLException { final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions ); - // supposed to be supported in JDBC 4.2 st.setObject( index, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE ); } @@ -95,7 +94,6 @@ protected void doBind( WrapperOptions wrapperOptions) throws SQLException { final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions ); - // supposed to be supported in JDBC 4.2 st.setObject( name, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE ); } }; diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampWithTimeZoneJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampWithTimeZoneJdbcType.java index 468452b69f7c..b2c977dd5420 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampWithTimeZoneJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampWithTimeZoneJdbcType.java @@ -16,10 +16,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; import java.sql.Types; import java.time.OffsetDateTime; -import java.util.Calendar; /** * Descriptor for {@link Types#TIMESTAMP_WITH_TIMEZONE TIMESTAMP_WITH_TIMEZONE} handling. @@ -74,24 +72,8 @@ protected void doBind( X value, int index, WrapperOptions options) throws SQLException { - try { - final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options ); - // supposed to be supported in JDBC 4.2 - st.setObject( index, dateTime, Types.TIMESTAMP_WITH_TIMEZONE ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - final Timestamp timestamp = javaType.unwrap( value, Timestamp.class, options ); - if ( value instanceof Calendar calendar ) { - st.setTimestamp( index, timestamp, calendar ); - } - else if ( options.getJdbcTimeZone() != null ) { - st.setTimestamp( index, timestamp, Calendar.getInstance( options.getJdbcTimeZone() ) ); - } - else { - st.setTimestamp( index, timestamp ); - } - } + final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options ); + st.setObject( index, dateTime, Types.TIMESTAMP_WITH_TIMEZONE ); } @Override @@ -101,24 +83,8 @@ protected void doBind( String name, WrapperOptions options) throws SQLException { - try { - final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options ); - // supposed to be supported in JDBC 4.2 - st.setObject( name, dateTime, Types.TIMESTAMP_WITH_TIMEZONE ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - final Timestamp timestamp = javaType.unwrap( value, Timestamp.class, options ); - if ( value instanceof Calendar calendar ) { - st.setTimestamp( name, timestamp, calendar ); - } - else if ( options.getJdbcTimeZone() != null ) { - st.setTimestamp( name, timestamp, Calendar.getInstance( options.getJdbcTimeZone() ) ); - } - else { - st.setTimestamp( name, timestamp ); - } - } + final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options ); + st.setObject( name, dateTime, Types.TIMESTAMP_WITH_TIMEZONE ); } }; } @@ -128,44 +94,17 @@ public ValueExtractor getExtractor(final JavaType javaType) { return new BasicExtractor<>( javaType, this ) { @Override protected X doExtract(ResultSet rs, int position, WrapperOptions options) throws SQLException { - try { - // supposed to be supported in JDBC 4.2 - return javaType.wrap( rs.getObject( position, OffsetDateTime.class ), options ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - return options.getJdbcTimeZone() != null ? - javaType.wrap( rs.getTimestamp( position, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) : - javaType.wrap( rs.getTimestamp( position ), options ); - } + return javaType.wrap( rs.getObject( position, OffsetDateTime.class ), options ); } @Override protected X doExtract(CallableStatement statement, int position, WrapperOptions options) throws SQLException { - try { - // supposed to be supported in JDBC 4.2 - return javaType.wrap( statement.getObject( position, OffsetDateTime.class ), options ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - return options.getJdbcTimeZone() != null ? - javaType.wrap( statement.getTimestamp( position, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) : - javaType.wrap( statement.getTimestamp( position ), options ); - } + return javaType.wrap( statement.getObject( position, OffsetDateTime.class ), options ); } @Override protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException { - try { - // supposed to be supported in JDBC 4.2 - return javaType.wrap( statement.getObject( name, OffsetDateTime.class ), options ); - } - catch (SQLException|AbstractMethodError e) { - // fall back to treating it as a JDBC Timestamp - return options.getJdbcTimeZone() != null ? - javaType.wrap( statement.getTimestamp( name, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) : - javaType.wrap( statement.getTimestamp( name ), options ); - } + return javaType.wrap( statement.getObject( name, OffsetDateTime.class ), options ); } }; }