From 93f1ed6e767332c1c769e1673b46235b30cc017c Mon Sep 17 00:00:00 2001 From: Cedomir Igaly Date: Wed, 31 May 2023 09:53:09 +0200 Subject: [PATCH] HHH-16591 - Replace thrre methods adaptToPreferredSqlTypeCodeForXXX with single adaptToPreferredSqlTypeCode --- .../process/spi/MetadataBuildingProcess.java | 83 +++++++------------ 1 file changed, 30 insertions(+), 53 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java b/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java index 9e61b23942..5aff6d965a 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java @@ -14,6 +14,7 @@ import java.time.OffsetDateTime; import java.time.OffsetTime; import java.time.ZonedDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -639,10 +640,12 @@ public class MetadataBuildingProcess { // add fallback type descriptors final int preferredSqlTypeCodeForUuid = getPreferredSqlTypeCodeForUuid( serviceRegistry ); if ( preferredSqlTypeCodeForUuid != SqlTypes.UUID ) { - adaptToPreferredSqlTypeCodeForUuid( + adaptToPreferredSqlTypeCode( typeConfiguration, jdbcTypeRegistry, - preferredSqlTypeCodeForUuid + preferredSqlTypeCodeForUuid, + UUID.class, + "uuid" ); } else { @@ -664,10 +667,12 @@ public class MetadataBuildingProcess { final int preferredSqlTypeCodeForDuration = getPreferredSqlTypeCodeForDuration( serviceRegistry ); if ( preferredSqlTypeCodeForDuration != SqlTypes.INTERVAL_SECOND ) { - adaptToPreferredSqlTypeCodeForDuration( + adaptToPreferredSqlTypeCode( typeConfiguration, jdbcTypeRegistry, - preferredSqlTypeCodeForDuration + preferredSqlTypeCodeForDuration, + Duration.class, + "duration" ); } else { @@ -712,7 +717,14 @@ public class MetadataBuildingProcess { } final int preferredSqlTypeCodeForInstant = getPreferredSqlTypeCodeForInstant( serviceRegistry ); if ( preferredSqlTypeCodeForInstant != SqlTypes.TIMESTAMP_UTC ) { - adaptToPreferredSqlTypeCodeForInstant( typeConfiguration, jdbcTypeRegistry, preferredSqlTypeCodeForInstant ); + adaptToPreferredSqlTypeCode( + typeConfiguration, + jdbcTypeRegistry, + preferredSqlTypeCodeForInstant, + Instant.class, + "instant", + "org.hibernate.type.InstantType" + ); } } @@ -730,59 +742,24 @@ public class MetadataBuildingProcess { // else warning? } - private static void adaptToPreferredSqlTypeCodeForUuid( + private static void adaptToPreferredSqlTypeCode( TypeConfiguration typeConfiguration, JdbcTypeRegistry jdbcTypeRegistry, - int preferredSqlTypeCodeForUuid) { + int preferredSqlTypeCode, + Class javaType, + String name, + String... additionalKeys) { final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry(); final BasicTypeRegistry basicTypeRegistry = typeConfiguration.getBasicTypeRegistry(); - final BasicType uuidType = new NamedBasicTypeImpl<>( - javaTypeRegistry.getDescriptor( UUID.class ), - jdbcTypeRegistry.getDescriptor( preferredSqlTypeCodeForUuid ), - "uuid" - ); - basicTypeRegistry.register( - uuidType, - UUID.class.getSimpleName(), - UUID.class.getName() - ); - } - - private static void adaptToPreferredSqlTypeCodeForDuration( - TypeConfiguration typeConfiguration, - JdbcTypeRegistry jdbcTypeRegistry, - int preferredSqlTypeCodeForDuration) { - final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry(); - final BasicTypeRegistry basicTypeRegistry = typeConfiguration.getBasicTypeRegistry(); - final BasicType durationType = new NamedBasicTypeImpl<>( - javaTypeRegistry.getDescriptor( Duration.class ), - jdbcTypeRegistry.getDescriptor( preferredSqlTypeCodeForDuration ), - "duration" - ); - basicTypeRegistry.register( - durationType, - Duration.class.getSimpleName(), - Duration.class.getName() - ); - } - - private static void adaptToPreferredSqlTypeCodeForInstant( - TypeConfiguration typeConfiguration, - JdbcTypeRegistry jdbcTypeRegistry, - int preferredSqlTypeCodeForInstant) { - final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry(); - final BasicTypeRegistry basicTypeRegistry = typeConfiguration.getBasicTypeRegistry(); - final BasicType instantType = new NamedBasicTypeImpl<>( - javaTypeRegistry.getDescriptor( Instant.class ), - jdbcTypeRegistry.getDescriptor( preferredSqlTypeCodeForInstant ), - "instant" - ); - basicTypeRegistry.register( - instantType, - "org.hibernate.type.InstantType", - Instant.class.getSimpleName(), - Instant.class.getName() + final BasicType basicType = new NamedBasicTypeImpl<>( + javaTypeRegistry.getDescriptor( javaType ), + jdbcTypeRegistry.getDescriptor( preferredSqlTypeCode ), + name ); + final String[] keys = Arrays.copyOf( additionalKeys, additionalKeys.length + 2 ); + keys[additionalKeys.length] = javaType.getSimpleName(); + keys[additionalKeys.length + 1] = javaType.getName(); + basicTypeRegistry.register( basicType, keys ); } private static void adaptTimeTypesToDefaultTimeZoneStorage(