Skip to content

Commit 6f47c3b

Browse files
Leoš Bittobeikov
authored andcommitted
HHH-19657: fix a bug in ArrayJdbcType which exposes itself in combination with setting hibernate.type.java_time_use_direct_jdbc=true
1 parent d5f2407 commit 6f47c3b

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,19 @@ protected String getElementTypeName(JavaType<?> javaType, SharedSessionContractI
119119
final String typeName =
120120
ddlTypeRegistry.getDescriptor( elementJdbcType.getDdlTypeCode() )
121121
.getTypeName( size, new BasicTypeImpl<>( elementJavaType, elementJdbcType), ddlTypeRegistry );
122-
// getTypeName for this case required length, etc, parameters.
123-
// Cut them out and use database defaults.
124-
final int cutIndex = typeName.indexOf( '(' );
125-
return cutIndex > 0 ? typeName.substring( 0, cutIndex ) : typeName;
122+
123+
final int cutIndexBegin = typeName.indexOf( '(' );
124+
if ( cutIndexBegin > 0 ) {
125+
final int cutIndexEnd = typeName.lastIndexOf( ')' );
126+
assert cutIndexEnd > cutIndexBegin;
127+
// getTypeName for this case required length, etc, parameters.
128+
// Cut them out and use database defaults.
129+
// e.g. "timestamp($p) with timezone" becomes "timestamp with timezone"
130+
return typeName.substring( 0, cutIndexBegin ) + typeName.substring( cutIndexEnd + 1 );
131+
}
132+
else {
133+
return typeName;
134+
}
126135
}
127136

128137
protected <T> Object[] getArray(BasicBinder<?> binder, ValueBinder<T> elementBinder, T value, WrapperOptions options)

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/javatime/GlobalJavaTimeJdbcTypeTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.time.LocalDate;
99
import java.time.LocalDateTime;
1010
import java.time.LocalTime;
11+
import java.time.OffsetDateTime;
1112
import java.time.temporal.ChronoUnit;
1213

1314
import org.hibernate.cfg.MappingSettings;
@@ -17,6 +18,7 @@
1718
import org.hibernate.dialect.Dialect;
1819
import org.hibernate.dialect.HANADialect;
1920
import org.hibernate.dialect.OracleDialect;
21+
import org.hibernate.dialect.PostgreSQLDialect;
2022
import org.hibernate.dialect.SybaseDialect;
2123
import org.hibernate.mapping.BasicValue;
2224
import org.hibernate.mapping.PersistentClass;
@@ -29,6 +31,7 @@
2931

3032
import org.hibernate.testing.orm.junit.DomainModel;
3133
import org.hibernate.testing.orm.junit.DomainModelScope;
34+
import org.hibernate.testing.orm.junit.RequiresDialect;
3235
import org.hibernate.testing.orm.junit.ServiceRegistry;
3336
import org.hibernate.testing.orm.junit.SessionFactory;
3437
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -203,6 +206,23 @@ void testLocalTime(SessionFactoryScope scope) {
203206
} );
204207
}
205208

209+
@Test
210+
@RequiresDialect(value = PostgreSQLDialect.class)
211+
void testArray(SessionFactoryScope scope) {
212+
final var offsetDateTime = OffsetDateTime.parse("1977-07-24T12:34:56+02:00");
213+
scope.inTransaction( session -> {
214+
final var nativeQuery = session.createNativeQuery(
215+
"WITH data AS (SELECT unnest(?) AS id, unnest(?) AS offset_date_time)"
216+
+ " INSERT INTO EntityWithJavaTimeValues (id, theOffsetDateTime) SELECT * FROM data"
217+
);
218+
nativeQuery.setParameter( 1, new int[] { 1 } );
219+
nativeQuery.setParameter( 2, new OffsetDateTime[] { offsetDateTime } );
220+
assertThat( nativeQuery.executeUpdate() ).isEqualTo( 1 );
221+
final var found = session.find( EntityWithJavaTimeValues.class, 1 );
222+
assertThat( found.theOffsetDateTime.toInstant() ).isEqualTo( offsetDateTime.toInstant() );
223+
} );
224+
}
225+
206226
@AfterEach
207227
void dropTestData(SessionFactoryScope scope) {
208228
scope.getSessionFactory().getSchemaManager().truncate();
@@ -215,6 +235,8 @@ public static class EntityWithJavaTimeValues {
215235
private Integer id;
216236
private String name;
217237

238+
private OffsetDateTime theOffsetDateTime;
239+
218240
private Instant theInstant;
219241

220242
private LocalDateTime theLocalDateTime;

0 commit comments

Comments
 (0)