From b09b27c7d69712480f304fbb6e543d91bf06598a Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 11 Sep 2024 19:42:14 +0200 Subject: [PATCH] HHH-18625 - Add Configurable#create(GeneratorCreationContext) https://hibernate.atlassian.net/browse/HHH-18625 --- .../boot/model/internal/GeneratorBinder.java | 20 +++++++---- .../model/naming/ObjectNameNormalizer.java | 5 +-- .../internal/JdbcEnvironmentInitiator.java | 1 - .../java/org/hibernate/id/Configurable.java | 9 +++++ .../factory/IdentifierGeneratorFactory.java | 20 +++++++++-- .../internal/IdentifierGeneratorUtil.java | 33 ++++++++++++++++++- .../StandardIdentifierGeneratorFactory.java | 8 +++-- 7 files changed, 79 insertions(+), 17 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java index 05782977b42d..d60ba173d99f 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java @@ -529,14 +529,20 @@ private static void checkVersionGenerationAlways(XProperty property, Generator g private static void callConfigure(GeneratorCreationContext creationContext, Generator generator) { if ( generator instanceof Configurable ) { + final Configurable configurable = (Configurable) generator; final Value value = creationContext.getProperty().getValue(); - ( (Configurable) generator ).configure( value.getType(), collectParameters( - (SimpleValue) value, - creationContext.getDatabase().getDialect(), - creationContext.getDefaultCatalog(), - creationContext.getDefaultSchema(), - creationContext.getPersistentClass().getRootClass() - ), creationContext.getServiceRegistry() ); + configurable.create( creationContext ); + configurable.configure( + value.getType(), + collectParameters( + (SimpleValue) value, + creationContext.getDatabase().getDialect(), + creationContext.getDefaultCatalog(), + creationContext.getDefaultSchema(), + creationContext.getPersistentClass().getRootClass() + ), + creationContext.getServiceRegistry() + ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ObjectNameNormalizer.java b/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ObjectNameNormalizer.java index 08155aee23dd..2e4d3d870f66 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ObjectNameNormalizer.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ObjectNameNormalizer.java @@ -42,10 +42,7 @@ protected Database database() { } public Identifier normalizeIdentifierQuoting(Identifier identifier) { - return getBuildingContext().getMetadataCollector() - .getDatabase() - .getJdbcEnvironment() - .getIdentifierHelper() + return database().getJdbcEnvironment().getIdentifierHelper() .normalizeQuoting( identifier ); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java index 7038b1aa9fc3..76335b785bfb 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java @@ -19,7 +19,6 @@ import org.hibernate.dialect.Dialect; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.jdbc.batch.spi.BatchBuilder; -import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator; import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo; diff --git a/hibernate-core/src/main/java/org/hibernate/id/Configurable.java b/hibernate-core/src/main/java/org/hibernate/id/Configurable.java index a8238430a2e2..34bfadad6f75 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/Configurable.java +++ b/hibernate-core/src/main/java/org/hibernate/id/Configurable.java @@ -11,6 +11,7 @@ import org.hibernate.MappingException; import org.hibernate.boot.model.relational.Database; import org.hibernate.boot.model.relational.SqlStringGenerationContext; +import org.hibernate.generator.GeneratorCreationContext; import org.hibernate.service.ServiceRegistry; import org.hibernate.type.Type; @@ -21,6 +22,14 @@ * @author Steve Ebersole */ public interface Configurable { + /** + * Called before {@link #configure(Type, Properties, ServiceRegistry)}, + * with an instance of {@link GeneratorCreationContext}. + * + * @since 6.6 + */ + default void create(GeneratorCreationContext creationContext) throws MappingException {} + /** * Configure this instance, given the value of parameters * specified by the user as XML {@code } elements and diff --git a/hibernate-core/src/main/java/org/hibernate/id/factory/IdentifierGeneratorFactory.java b/hibernate-core/src/main/java/org/hibernate/id/factory/IdentifierGeneratorFactory.java index ec21727bbf2a..af06fb73e8ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/factory/IdentifierGeneratorFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/id/factory/IdentifierGeneratorFactory.java @@ -9,7 +9,7 @@ import java.util.Properties; import org.hibernate.Incubating; -import org.hibernate.dialect.Dialect; +import org.hibernate.generator.GeneratorCreationContext; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.factory.spi.GeneratorDefinitionResolver; import org.hibernate.service.Service; @@ -71,5 +71,21 @@ Generator createIdentifierGenerator( * @deprecated use {@link #createIdentifierGenerator(GenerationType, String, String, JavaType, Properties, GeneratorDefinitionResolver)} */ @Deprecated(since = "6.0") - Generator createIdentifierGenerator(String strategy, Type type, Properties parameters); + Generator createIdentifierGenerator(String strategy, Type type, GeneratorCreationContext creationContext, Properties parameters); + + /** + * Given a strategy, retrieve the appropriate identifier generator instance. + * + * @param strategy The generation strategy. + * @param type The mapping type for the identifier values. + * @param parameters Any parameters properties given in the generator mapping. + * + * @return The appropriate generator instance. + * + * @deprecated use {@link #createIdentifierGenerator(GenerationType, String, String, JavaType, Properties, GeneratorDefinitionResolver)} + */ + @Deprecated(since = "6.0") + default Generator createIdentifierGenerator(String strategy, Type type, Properties parameters) { + return createIdentifierGenerator( strategy, type, null, parameters ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java index 212cb3970a9e..b2f08bd9e1aa 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java +++ b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java @@ -6,10 +6,12 @@ */ package org.hibernate.id.factory.internal; +import org.hibernate.boot.model.relational.Database; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.StandardConverters; +import org.hibernate.generator.GeneratorCreationContext; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.OptimizableGenerator; import org.hibernate.id.PersistentIdentifierGenerator; @@ -17,10 +19,13 @@ import org.hibernate.id.enhanced.SingleNamingStrategy; import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.mapping.Column; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; import org.hibernate.mapping.RootClass; import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.Table; import org.hibernate.generator.Generator; +import org.hibernate.service.ServiceRegistry; import java.util.Map; import java.util.Properties; @@ -37,6 +42,32 @@ public static Generator createLegacyIdentifierGenerator( return identifierGeneratorFactory.createIdentifierGenerator( simpleValue.getIdentifierGeneratorStrategy(), simpleValue.getType(), + new GeneratorCreationContext() { + @Override + public Database getDatabase() { + return simpleValue.getMetadata().getDatabase(); + } + @Override + public ServiceRegistry getServiceRegistry() { + return simpleValue.getServiceRegistry(); + } + @Override + public String getDefaultCatalog() { + return null; + } + @Override + public String getDefaultSchema() { + return null; + } + @Override + public PersistentClass getPersistentClass() { + return rootClass; + } + @Override + public Property getProperty() { + return rootClass.getIdentifierProperty(); + } + }, collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass ) ); } @@ -48,7 +79,7 @@ public static Properties collectParameters( String defaultSchema, RootClass rootClass) { final ConfigurationService configService = - simpleValue.getMetadata().getMetadataBuildingOptions().getServiceRegistry() + simpleValue.getServiceRegistry() .requireService( ConfigurationService.class ); final Properties params = new Properties(); diff --git a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/StandardIdentifierGeneratorFactory.java b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/StandardIdentifierGeneratorFactory.java index ed30caee7f94..b332faec0f73 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/StandardIdentifierGeneratorFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/StandardIdentifierGeneratorFactory.java @@ -19,6 +19,7 @@ import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.generator.Generator; +import org.hibernate.generator.GeneratorCreationContext; import org.hibernate.id.Assigned; import org.hibernate.id.Configurable; import org.hibernate.id.ForeignGenerator; @@ -206,7 +207,8 @@ private Dialect getDialect() { } @Override @Deprecated - public Generator createIdentifierGenerator(String strategy, Type type, Properties parameters) { + public Generator createIdentifierGenerator( + String strategy, Type type, GeneratorCreationContext creationContext, Properties parameters) { try { final Class clazz = getIdentifierGeneratorClass( strategy ); final Generator identifierGenerator; @@ -222,7 +224,9 @@ public Generator createIdentifierGenerator(String strategy, Type type, Propertie } if ( identifierGenerator instanceof Configurable ) { - ( (Configurable) identifierGenerator ).configure( type, parameters, serviceRegistry ); + final Configurable configurable = (Configurable) identifierGenerator; + configurable.create( creationContext ); + configurable.configure( type, parameters, serviceRegistry ); } return identifierGenerator; }