From eccc1a89c5d3d2ec92d344962613b9c9d2bd79d6 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 16 Dec 2022 08:36:52 +0100 Subject: [PATCH] Ensure TypeContributor registered JdbcType has precedence over fallback and preferred types --- .../process/spi/MetadataBuildingProcess.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 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 870cb861f9..33694e38ed 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 @@ -391,6 +391,11 @@ public class MetadataBuildingProcess { // add Dialect contributed types final Dialect dialect = options.getServiceRegistry().getService( JdbcServices.class ).getDialect(); dialect.contributeTypes( typeContributions, options.getServiceRegistry() ); + // Capture the dialect configured JdbcTypes so that we can detect if a TypeContributor overwrote them, + // which has precedence over the fallback and preferred type registrations + final JdbcType dialectUuidDescriptor = jdbcTypeRegistry.findDescriptor( SqlTypes.UUID ); + final JdbcType dialectArrayDescriptor = jdbcTypeRegistry.findDescriptor( SqlTypes.ARRAY ); + final JdbcType dialectIntervalDescriptor = jdbcTypeRegistry.findDescriptor( SqlTypes.INTERVAL_SECOND ); // add TypeContributor contributed types. for ( TypeContributor contributor : classLoaderService.loadJavaServices( TypeContributor.class ) ) { @@ -400,23 +405,38 @@ public class MetadataBuildingProcess { // add fallback type descriptors final int preferredSqlTypeCodeForUuid = getPreferredSqlTypeCodeForUuid( serviceRegistry ); if ( preferredSqlTypeCodeForUuid != SqlTypes.UUID ) { - adaptToPreferredSqlTypeCode( jdbcTypeRegistry, SqlTypes.UUID, preferredSqlTypeCodeForUuid ); + adaptToPreferredSqlTypeCode( + jdbcTypeRegistry, + dialectUuidDescriptor, + SqlTypes.UUID, + preferredSqlTypeCodeForUuid + ); } else { addFallbackIfNecessary( jdbcTypeRegistry, SqlTypes.UUID, SqlTypes.BINARY ); } final int preferredSqlTypeCodeForArray = getPreferredSqlTypeCodeForArray( serviceRegistry ); - if ( preferredSqlTypeCodeForArray == SqlTypes.ARRAY ) { - adaptToPreferredSqlTypeCode( jdbcTypeRegistry, null, SqlTypes.ARRAY, SqlTypes.VARBINARY ); + if ( preferredSqlTypeCodeForArray != SqlTypes.ARRAY ) { + adaptToPreferredSqlTypeCode( + jdbcTypeRegistry, + dialectArrayDescriptor, + SqlTypes.ARRAY, + preferredSqlTypeCodeForArray + ); } else { - adaptToPreferredSqlTypeCode( jdbcTypeRegistry, SqlTypes.ARRAY, preferredSqlTypeCodeForArray ); + addFallbackIfNecessary( jdbcTypeRegistry, SqlTypes.ARRAY, SqlTypes.VARBINARY ); } final int preferredSqlTypeCodeForDuration = getPreferredSqlTypeCodeForDuration( serviceRegistry ); if ( preferredSqlTypeCodeForDuration != SqlTypes.INTERVAL_SECOND ) { - adaptToPreferredSqlTypeCode( jdbcTypeRegistry, SqlTypes.INTERVAL_SECOND, preferredSqlTypeCodeForDuration ); + adaptToPreferredSqlTypeCode( + jdbcTypeRegistry, + dialectIntervalDescriptor, + SqlTypes.INTERVAL_SECOND, + preferredSqlTypeCodeForDuration + ); } else { addFallbackIfNecessary( jdbcTypeRegistry, SqlTypes.INTERVAL_SECOND, SqlTypes.NUMERIC ); @@ -471,18 +491,6 @@ public class MetadataBuildingProcess { } } - private static void adaptToPreferredSqlTypeCode( - JdbcTypeRegistry jdbcTypeRegistry, - int defaultSqlTypeCode, - int preferredSqlTypeCode) { - adaptToPreferredSqlTypeCode( - jdbcTypeRegistry, - jdbcTypeRegistry.findDescriptor( defaultSqlTypeCode ), - defaultSqlTypeCode, - preferredSqlTypeCode - ); - } - private static void adaptToPreferredSqlTypeCode( JdbcTypeRegistry jdbcTypeRegistry, JdbcType dialectUuidDescriptor, @@ -494,6 +502,7 @@ public class MetadataBuildingProcess { jdbcTypeRegistry.getDescriptor( preferredSqlTypeCode ) ); } + // else warning? } private static void adaptToPreferredSqlTypeCodeForInstant(