Skip to content

Commit 452b794

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 c83c6a5 commit 452b794

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,18 @@ protected String getElementTypeName(JavaType<?> javaType, SharedSessionContractI
126126
.getSizeStrategy()
127127
.resolveSize( elementJdbcType, elementJavaType, null, null, null );
128128
final DdlTypeRegistry ddlTypeRegistry = session.getTypeConfiguration().getDdlTypeRegistry();
129-
final String typeName = ddlTypeRegistry.getDescriptor( elementJdbcType.getDdlTypeCode() )
130-
.getTypeName( size, new BasicTypeImpl<>( elementJavaType, elementJdbcType), ddlTypeRegistry );
131-
int cutIndex = typeName.indexOf( '(' );
132-
if ( cutIndex > 0 ) {
129+
final String typeName =
130+
ddlTypeRegistry.getDescriptor( elementJdbcType.getDdlTypeCode() )
131+
.getTypeName( size, new BasicTypeImpl<>( elementJavaType, elementJdbcType), ddlTypeRegistry );
132+
133+
final int cutIndexBegin = typeName.indexOf( '(' );
134+
if ( cutIndexBegin > 0 ) {
135+
final int cutIndexEnd = typeName.lastIndexOf( ')' );
136+
assert cutIndexEnd > cutIndexBegin;
133137
// getTypeName for this case required length, etc, parameters.
134138
// Cut them out and use database defaults.
135-
return typeName.substring( 0, cutIndex );
139+
// e.g. "timestamp($p) with timezone" becomes "timestamp with timezone"
140+
return typeName.substring( 0, cutIndexBegin ) + typeName.substring( cutIndexEnd + 1 );
136141
}
137142
else {
138143
return typeName;

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.inTransaction( (session) -> {
@@ -217,6 +237,8 @@ public static class EntityWithJavaTimeValues {
217237
private Integer id;
218238
private String name;
219239

240+
private OffsetDateTime theOffsetDateTime;
241+
220242
private Instant theInstant;
221243

222244
private LocalDateTime theLocalDateTime;

0 commit comments

Comments
 (0)