Skip to content

Commit d8a5249

Browse files
committed
Simplify handling of Instant and OffsetDateTime and remove incorrect/dubious comments about JDBC 4.2
- This removes support for noncompliant jdbc 4.2 drivers for OffsetDateTime. - This removes nonstandard code for Instant which duplicates the standard code that has to be maintained anyway.
1 parent 41167f3 commit d8a5249

File tree

3 files changed

+22
-108
lines changed

3 files changed

+22
-108
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsInstantJdbcType.java

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,9 @@ protected void doBind(
8484
int index,
8585
WrapperOptions wrapperOptions) throws SQLException {
8686
final Instant instant = javaType.unwrap( value, Instant.class, wrapperOptions );
87-
try {
88-
// supposed to be supported in JDBC 4.2
89-
st.setObject( index, instant, Types.TIMESTAMP_WITH_TIMEZONE );
90-
}
91-
catch (SQLException|AbstractMethodError e) {
92-
// fall back to treating it as a JDBC Timestamp
93-
st.setTimestamp( index, Timestamp.from( instant ), UTC_CALENDAR );
94-
}
87+
// some jdbc drivers may support java.time.Instant directly but
88+
// this is the only standard support in the jdbc 4.2 specification
89+
st.setTimestamp( index, Timestamp.from( instant ), UTC_CALENDAR );
9590
}
9691

9792
@Override
@@ -102,14 +97,9 @@ protected void doBind(
10297
WrapperOptions wrapperOptions)
10398
throws SQLException {
10499
final Instant instant = javaType.unwrap( value, Instant.class, wrapperOptions );
105-
try {
106-
// supposed to be supported in JDBC 4.2
107-
st.setObject( name, instant, Types.TIMESTAMP_WITH_TIMEZONE );
108-
}
109-
catch (SQLException|AbstractMethodError e) {
110-
// fall back to treating it as a JDBC Timestamp
111-
st.setTimestamp( name, Timestamp.from( instant ), UTC_CALENDAR );
112-
}
100+
// some jdbc drivers may support java.time.Instant directly but
101+
// this is the only standard support in the jdbc 4.2 specification
102+
st.setTimestamp( name, Timestamp.from( instant ), UTC_CALENDAR );
113103
}
114104
};
115105
}
@@ -119,38 +109,23 @@ public <X> ValueExtractor<X> getExtractor(final JavaType<X> javaType) {
119109
return new BasicExtractor<>( javaType, this ) {
120110
@Override
121111
protected X doExtract(ResultSet rs, int position, WrapperOptions wrapperOptions) throws SQLException {
122-
try {
123-
// supposed to be supported in JDBC 4.2
124-
return javaType.wrap( rs.getObject( position, Instant.class ), wrapperOptions );
125-
}
126-
catch (SQLException|AbstractMethodError e) {
127-
// fall back to treating it as a JDBC Timestamp
128-
return javaType.wrap( rs.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
129-
}
112+
// some jdbc drivers may support java.time.Instant directly but
113+
// this is the only standard support in the jdbc 4.2 specification
114+
return javaType.wrap( rs.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
130115
}
131116

132117
@Override
133118
protected X doExtract(CallableStatement statement, int position, WrapperOptions wrapperOptions) throws SQLException {
134-
try {
135-
// supposed to be supported in JDBC 4.2
136-
return javaType.wrap( statement.getObject( position, Instant.class ), wrapperOptions );
137-
}
138-
catch (SQLException|AbstractMethodError e) {
139-
// fall back to treating it as a JDBC Timestamp
140-
return javaType.wrap( statement.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
141-
}
119+
// some jdbc drivers may support java.time.Instant directly but
120+
// this is the only standard support in the jdbc 4.2 specification
121+
return javaType.wrap( statement.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
142122
}
143123

144124
@Override
145125
protected X doExtract(CallableStatement statement, String name, WrapperOptions wrapperOptions) throws SQLException {
146-
try {
147-
// supposed to be supported in JDBC 4.2
148-
return javaType.wrap( statement.getObject( name, Instant.class ), wrapperOptions );
149-
}
150-
catch (SQLException|AbstractMethodError e) {
151-
// fall back to treating it as a JDBC Timestamp
152-
return javaType.wrap( statement.getTimestamp( name, UTC_CALENDAR ), wrapperOptions );
153-
}
126+
// some jdbc drivers may support java.time.Instant directly but
127+
// this is the only standard support in the jdbc 4.2 specification
128+
return javaType.wrap( statement.getTimestamp( name, UTC_CALENDAR ), wrapperOptions );
154129
}
155130
};
156131
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampUtcAsOffsetDateTimeJdbcType.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ protected void doBind(
8383
int index,
8484
WrapperOptions wrapperOptions) throws SQLException {
8585
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions );
86-
// supposed to be supported in JDBC 4.2
8786
st.setObject( index, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE );
8887
}
8988

@@ -95,7 +94,6 @@ protected void doBind(
9594
WrapperOptions wrapperOptions)
9695
throws SQLException {
9796
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions );
98-
// supposed to be supported in JDBC 4.2
9997
st.setObject( name, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE );
10098
}
10199
};

hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/TimestampWithTimeZoneJdbcType.java

Lines changed: 7 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,8 @@ protected void doBind(
7474
X value,
7575
int index,
7676
WrapperOptions options) throws SQLException {
77-
try {
78-
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
79-
// supposed to be supported in JDBC 4.2
80-
st.setObject( index, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
81-
}
82-
catch (SQLException|AbstractMethodError e) {
83-
// fall back to treating it as a JDBC Timestamp
84-
final Timestamp timestamp = javaType.unwrap( value, Timestamp.class, options );
85-
if ( value instanceof Calendar calendar ) {
86-
st.setTimestamp( index, timestamp, calendar );
87-
}
88-
else if ( options.getJdbcTimeZone() != null ) {
89-
st.setTimestamp( index, timestamp, Calendar.getInstance( options.getJdbcTimeZone() ) );
90-
}
91-
else {
92-
st.setTimestamp( index, timestamp );
93-
}
94-
}
77+
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
78+
st.setObject( index, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
9579
}
9680

9781
@Override
@@ -101,24 +85,8 @@ protected void doBind(
10185
String name,
10286
WrapperOptions options)
10387
throws SQLException {
104-
try {
105-
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
106-
// supposed to be supported in JDBC 4.2
107-
st.setObject( name, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
108-
}
109-
catch (SQLException|AbstractMethodError e) {
110-
// fall back to treating it as a JDBC Timestamp
111-
final Timestamp timestamp = javaType.unwrap( value, Timestamp.class, options );
112-
if ( value instanceof Calendar calendar ) {
113-
st.setTimestamp( name, timestamp, calendar );
114-
}
115-
else if ( options.getJdbcTimeZone() != null ) {
116-
st.setTimestamp( name, timestamp, Calendar.getInstance( options.getJdbcTimeZone() ) );
117-
}
118-
else {
119-
st.setTimestamp( name, timestamp );
120-
}
121-
}
88+
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
89+
st.setObject( name, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
12290
}
12391
};
12492
}
@@ -128,44 +96,17 @@ public <X> ValueExtractor<X> getExtractor(final JavaType<X> javaType) {
12896
return new BasicExtractor<>( javaType, this ) {
12997
@Override
13098
protected X doExtract(ResultSet rs, int position, WrapperOptions options) throws SQLException {
131-
try {
132-
// supposed to be supported in JDBC 4.2
133-
return javaType.wrap( rs.getObject( position, OffsetDateTime.class ), options );
134-
}
135-
catch (SQLException|AbstractMethodError e) {
136-
// fall back to treating it as a JDBC Timestamp
137-
return options.getJdbcTimeZone() != null ?
138-
javaType.wrap( rs.getTimestamp( position, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) :
139-
javaType.wrap( rs.getTimestamp( position ), options );
140-
}
99+
return javaType.wrap( rs.getObject( position, OffsetDateTime.class ), options );
141100
}
142101

143102
@Override
144103
protected X doExtract(CallableStatement statement, int position, WrapperOptions options) throws SQLException {
145-
try {
146-
// supposed to be supported in JDBC 4.2
147-
return javaType.wrap( statement.getObject( position, OffsetDateTime.class ), options );
148-
}
149-
catch (SQLException|AbstractMethodError e) {
150-
// fall back to treating it as a JDBC Timestamp
151-
return options.getJdbcTimeZone() != null ?
152-
javaType.wrap( statement.getTimestamp( position, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) :
153-
javaType.wrap( statement.getTimestamp( position ), options );
154-
}
104+
return javaType.wrap( statement.getObject( position, OffsetDateTime.class ), options );
155105
}
156106

157107
@Override
158108
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
159-
try {
160-
// supposed to be supported in JDBC 4.2
161-
return javaType.wrap( statement.getObject( name, OffsetDateTime.class ), options );
162-
}
163-
catch (SQLException|AbstractMethodError e) {
164-
// fall back to treating it as a JDBC Timestamp
165-
return options.getJdbcTimeZone() != null ?
166-
javaType.wrap( statement.getTimestamp( name, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) :
167-
javaType.wrap( statement.getTimestamp( name ), options );
168-
}
109+
return javaType.wrap( statement.getObject( name, OffsetDateTime.class ), options );
169110
}
170111
};
171112
}

0 commit comments

Comments
 (0)