Skip to content

Commit d38b2db

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 d861905 commit d38b2db

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
@@ -128,13 +128,18 @@ protected String getElementTypeName(JavaType<?> javaType, SharedSessionContractI
128128
.getSizeStrategy()
129129
.resolveSize( elementJdbcType, elementJavaType, null, null, null );
130130
final DdlTypeRegistry ddlTypeRegistry = session.getTypeConfiguration().getDdlTypeRegistry();
131-
final String typeName = ddlTypeRegistry.getDescriptor( elementJdbcType.getDdlTypeCode() )
132-
.getTypeName( size, new BasicTypeImpl<>( elementJavaType, elementJdbcType), ddlTypeRegistry );
133-
int cutIndex = typeName.indexOf( '(' );
134-
if ( cutIndex > 0 ) {
131+
final String typeName =
132+
ddlTypeRegistry.getDescriptor( elementJdbcType.getDdlTypeCode() )
133+
.getTypeName( size, new BasicTypeImpl<>( elementJavaType, elementJdbcType), ddlTypeRegistry );
134+
135+
final int cutIndexBegin = typeName.indexOf( '(' );
136+
if ( cutIndexBegin > 0 ) {
137+
final int cutIndexEnd = typeName.lastIndexOf( ')' );
138+
assert cutIndexEnd > cutIndexBegin;
135139
// getTypeName for this case required length, etc, parameters.
136140
// Cut them out and use database defaults.
137-
return typeName.substring( 0, cutIndex );
141+
// e.g. "timestamp($p) with timezone" becomes "timestamp with timezone"
142+
return typeName.substring( 0, cutIndexBegin ) + typeName.substring( cutIndexEnd + 1 );
138143
}
139144
else {
140145
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
@@ -10,6 +10,7 @@
1010
import java.time.LocalDate;
1111
import java.time.LocalDateTime;
1212
import java.time.LocalTime;
13+
import java.time.OffsetDateTime;
1314
import java.time.temporal.ChronoUnit;
1415

1516
import org.hibernate.cfg.MappingSettings;
@@ -19,6 +20,7 @@
1920
import org.hibernate.dialect.Dialect;
2021
import org.hibernate.dialect.HANADialect;
2122
import org.hibernate.dialect.OracleDialect;
23+
import org.hibernate.dialect.PostgreSQLDialect;
2224
import org.hibernate.dialect.SybaseDialect;
2325
import org.hibernate.mapping.BasicValue;
2426
import org.hibernate.mapping.PersistentClass;
@@ -31,6 +33,7 @@
3133

3234
import org.hibernate.testing.orm.junit.DomainModel;
3335
import org.hibernate.testing.orm.junit.DomainModelScope;
36+
import org.hibernate.testing.orm.junit.RequiresDialect;
3437
import org.hibernate.testing.orm.junit.ServiceRegistry;
3538
import org.hibernate.testing.orm.junit.SessionFactory;
3639
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -204,6 +207,23 @@ void testLocalTime(SessionFactoryScope scope) {
204207
} );
205208
}
206209

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

241+
private OffsetDateTime theOffsetDateTime;
242+
221243
private Instant theInstant;
222244

223245
private LocalDateTime theLocalDateTime;

0 commit comments

Comments
 (0)