From 9d3b3d1c8a71c989fe2168ec7998811939fc56f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 4 Nov 2021 10:38:47 +0100 Subject: [PATCH] HHH-14921 Clarify the ability of IdentifierGenerator to be configured and to register exportables --- .../InFlightMetadataCollectorImpl.java | 5 +-- .../main/java/org/hibernate/id/Assigned.java | 2 +- .../java/org/hibernate/id/Configurable.java | 3 ++ .../org/hibernate/id/ForeignGenerator.java | 2 +- .../org/hibernate/id/IdentifierGenerator.java | 35 +++++++++++++++++-- .../org/hibernate/id/IncrementGenerator.java | 2 +- .../id/MultipleHiLoPerTableGenerator.java | 3 +- .../id/PersistentIdentifierGenerator.java | 12 ++++--- .../org/hibernate/id/SelectGenerator.java | 2 +- .../org/hibernate/id/SequenceGenerator.java | 2 +- .../java/org/hibernate/id/UUIDGenerator.java | 2 +- .../org/hibernate/id/UUIDHexGenerator.java | 2 +- .../id/enhanced/SequenceStyleGenerator.java | 3 +- .../hibernate/id/enhanced/TableGenerator.java | 3 +- .../DefaultIdentifierGeneratorFactory.java | 5 +-- .../java/org/hibernate/mapping/Component.java | 5 +-- 16 files changed, 57 insertions(+), 31 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java index 568ef96498..5b780c15ee 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java @@ -45,7 +45,6 @@ import org.hibernate.boot.model.naming.ImplicitIndexNameSource; import org.hibernate.boot.model.naming.ImplicitUniqueKeyNameSource; import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject; import org.hibernate.boot.model.relational.Database; -import org.hibernate.boot.model.relational.ExportableProducer; import org.hibernate.boot.model.relational.Namespace; import org.hibernate.boot.model.relational.QualifiedTableName; import org.hibernate.boot.model.source.internal.ImplicitColumnNamingSecondPass; @@ -2249,9 +2248,7 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector entityBinding ); - if ( ig instanceof ExportableProducer ) { - ( (ExportableProducer) ig ).registerExportables( getDatabase() ); - } + ig.registerExportables( getDatabase() ); } catch (MappingException e) { // ignore this for now. The reasoning being "non-reflective" binding as needed diff --git a/hibernate-core/src/main/java/org/hibernate/id/Assigned.java b/hibernate-core/src/main/java/org/hibernate/id/Assigned.java index e22faa0f7d..cfffdcc210 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/Assigned.java +++ b/hibernate-core/src/main/java/org/hibernate/id/Assigned.java @@ -23,7 +23,7 @@ import org.hibernate.type.Type; * * @author Gavin King */ -public class Assigned implements IdentifierGenerator, Configurable { +public class Assigned implements IdentifierGenerator { private String entityName; public Serializable generate(SharedSessionContractImplementor session, Object obj) throws HibernateException { 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 7ffdcc9700..87d53fba0d 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/Configurable.java +++ b/hibernate-core/src/main/java/org/hibernate/id/Configurable.java @@ -15,11 +15,14 @@ import org.hibernate.type.Type; /** * An {@link IdentifierGenerator} that supports "configuration". * + * @deprecated All methods are already defined in {@link IdentifierGenerator}. + * Just implement {@link IdentifierGenerator}. * @see IdentifierGenerator * * @author Gavin King * @author Steve Ebersole */ +@Deprecated public interface Configurable { /** * Configure this instance, given the value of parameters diff --git a/hibernate-core/src/main/java/org/hibernate/id/ForeignGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/ForeignGenerator.java index d7a00d37a9..6528d81c13 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/ForeignGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/ForeignGenerator.java @@ -34,7 +34,7 @@ import static org.hibernate.internal.CoreLogging.messageLogger; * * @author Gavin King */ -public class ForeignGenerator implements IdentifierGenerator, Configurable { +public class ForeignGenerator implements IdentifierGenerator { private static final CoreMessageLogger LOG = messageLogger( ForeignGenerator.class ); private String entityName; diff --git a/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java index 51bc8a355e..ca3da8b327 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java @@ -7,10 +7,16 @@ package org.hibernate.id; import java.io.Serializable; +import java.util.Properties; import javax.persistence.GeneratedValue; import org.hibernate.HibernateException; +import org.hibernate.MappingException; +import org.hibernate.boot.model.relational.Database; +import org.hibernate.boot.model.relational.ExportableProducer; import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.type.Type; /** * The general contract between a class that generates unique @@ -29,9 +35,8 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor; * @author Gavin King * * @see PersistentIdentifierGenerator - * @see Configurable */ -public interface IdentifierGenerator { +public interface IdentifierGenerator extends Configurable, ExportableProducer { /** * The configuration parameter holding the entity name */ @@ -48,6 +53,32 @@ public interface IdentifierGenerator { */ String GENERATOR_NAME = "GENERATOR_NAME"; + /** + * Configure this instance, given the value of parameters + * specified by the user as <param> elements. + *

+ * This method is called just once, following instantiation, and before {@link #registerExportables(Database)}. + * + * @param type The id property type descriptor + * @param params param values, keyed by parameter name + * @param serviceRegistry Access to service that may be needed. + * @throws MappingException If configuration fails. + */ + @Override + default void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { + } + + /** + * Register database objects used by this identifier generator, e.g. sequences, tables, etc. + *

+ * This method is called just once, after {@link #configure(Type, Properties, ServiceRegistry)}. + * + * @param database The database instance + */ + @Override + default void registerExportables(Database database) { + } + /** * Generate a new identifier. * diff --git a/hibernate-core/src/main/java/org/hibernate/id/IncrementGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/IncrementGenerator.java index 68ab1517aa..6f2261a4de 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/IncrementGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/IncrementGenerator.java @@ -38,7 +38,7 @@ import org.hibernate.type.Type; * @author Steve Ebersole * @author Brett Meyer */ -public class IncrementGenerator implements IdentifierGenerator, Configurable { +public class IncrementGenerator implements IdentifierGenerator { private static final CoreMessageLogger LOG = CoreLogging.messageLogger( IncrementGenerator.class ); private Class returnClass; diff --git a/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java index 6c7c1000d8..eb891e5ae3 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java @@ -78,7 +78,7 @@ import org.hibernate.type.Type; * @deprecated Use {@link org.hibernate.id.enhanced.TableGenerator} instead. */ @Deprecated -public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenerator, Configurable { +public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenerator { private static final CoreMessageLogger LOG = CoreLogging.messageLogger( MultipleHiLoPerTableGenerator.class ); public static final String ID_TABLE = "table"; @@ -253,6 +253,7 @@ public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenera } } + @Override @SuppressWarnings({"StatementWithEmptyBody", "deprecation"}) public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { returnClass = type.getReturnedClass(); diff --git a/hibernate-core/src/main/java/org/hibernate/id/PersistentIdentifierGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/PersistentIdentifierGenerator.java index 6529124f98..cc81847f51 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/PersistentIdentifierGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/PersistentIdentifierGenerator.java @@ -6,23 +6,25 @@ */ package org.hibernate.id; +import java.util.Properties; + import org.hibernate.HibernateException; -import org.hibernate.boot.model.relational.ExportableProducer; import org.hibernate.dialect.Dialect; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.type.Type; /** * An IdentifierGenerator that requires creation of database objects. *

- * All PersistentIdentifierGenerators that also implement - * Configurable have access to a special mapping parameter: schema + * All PersistentIdentifierGenerators have access to a special mapping parameter + * in their {@link #configure(Type, Properties, ServiceRegistry)} method: schema * * @author Gavin King * @author Steve Ebersole * * @see IdentifierGenerator - * @see Configurable */ -public interface PersistentIdentifierGenerator extends IdentifierGenerator, ExportableProducer { +public interface PersistentIdentifierGenerator extends IdentifierGenerator { /** * The configuration parameter holding the schema name diff --git a/hibernate-core/src/main/java/org/hibernate/id/SelectGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/SelectGenerator.java index eb7521309e..40c0b0eb66 100755 --- a/hibernate-core/src/main/java/org/hibernate/id/SelectGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/SelectGenerator.java @@ -31,7 +31,7 @@ import org.hibernate.type.Type; * * @author Gavin King */ -public class SelectGenerator extends AbstractPostInsertGenerator implements Configurable { +public class SelectGenerator extends AbstractPostInsertGenerator { private String uniqueKeyPropertyName; @Override diff --git a/hibernate-core/src/main/java/org/hibernate/id/SequenceGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/SequenceGenerator.java index cf82f58765..1f68fa056e 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/SequenceGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/SequenceGenerator.java @@ -45,7 +45,7 @@ import org.jboss.logging.Logger; */ @Deprecated public class SequenceGenerator - implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, Configurable { + implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator { private static final Logger LOG = Logger.getLogger( SequenceGenerator.class.getName() ); diff --git a/hibernate-core/src/main/java/org/hibernate/id/UUIDGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/UUIDGenerator.java index 0b86ee15ce..a18597b1fd 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/UUIDGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/UUIDGenerator.java @@ -39,7 +39,7 @@ import org.hibernate.type.descriptor.java.UUIDTypeDescriptor; * * @author Steve Ebersole */ -public class UUIDGenerator implements IdentifierGenerator, Configurable { +public class UUIDGenerator implements IdentifierGenerator { public static final String UUID_GEN_STRATEGY = "uuid_gen_strategy"; public static final String UUID_GEN_STRATEGY_CLASS = "uuid_gen_strategy_class"; diff --git a/hibernate-core/src/main/java/org/hibernate/id/UUIDHexGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/UUIDHexGenerator.java index a51c6fd9af..a0370886de 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/UUIDHexGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/UUIDHexGenerator.java @@ -29,7 +29,7 @@ import org.hibernate.type.Type; * * @author Gavin King */ -public class UUIDHexGenerator extends AbstractUUIDGenerator implements Configurable { +public class UUIDHexGenerator extends AbstractUUIDGenerator { private static final CoreMessageLogger LOG = CoreLogging.messageLogger( UUIDHexGenerator.class ); private static boolean WARNED; diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java index d3d7de7e5c..27b89170ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java @@ -24,7 +24,6 @@ import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.id.BulkInsertionCapableIdentifierGenerator; -import org.hibernate.id.Configurable; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.SequenceMismatchStrategy; @@ -101,7 +100,7 @@ import org.jboss.logging.Logger; * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ public class SequenceStyleGenerator - implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, Configurable { + implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator { private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java index 5a96109195..d817777b55 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java @@ -36,7 +36,6 @@ import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.SqlStatementLogger; import org.hibernate.engine.spi.SessionEventListenerManager; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.id.Configurable; import org.hibernate.id.ExportableColumn; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGeneratorHelper; @@ -129,7 +128,7 @@ import org.jboss.logging.Logger; * * @author Steve Ebersole */ -public class TableGenerator implements PersistentIdentifierGenerator, Configurable { +public class TableGenerator implements PersistentIdentifierGenerator { private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, TableGenerator.class.getName() diff --git a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java index 5d433606e6..b08e650211 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/DefaultIdentifierGeneratorFactory.java @@ -19,7 +19,6 @@ import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.id.Assigned; -import org.hibernate.id.Configurable; import org.hibernate.id.ForeignGenerator; import org.hibernate.id.GUIDGenerator; import org.hibernate.id.IdentifierGenerator; @@ -140,9 +139,7 @@ public class DefaultIdentifierGeneratorFactory FallbackBeanInstanceProducer.INSTANCE ).getBeanInstance(); } - if ( identifierGenerator instanceof Configurable ) { - ( ( Configurable ) identifierGenerator ).configure( type, config, serviceRegistry ); - } + identifierGenerator.configure( type, config, serviceRegistry ); return identifierGenerator; } catch ( Exception e ) { diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Component.java b/hibernate-core/src/main/java/org/hibernate/mapping/Component.java index af85ad6fbb..105d59f50b 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Component.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Component.java @@ -16,7 +16,6 @@ import java.util.Map; import org.hibernate.EntityMode; import org.hibernate.MappingException; import org.hibernate.boot.model.relational.Database; -import org.hibernate.boot.model.relational.ExportableProducer; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.boot.spi.MetadataBuildingContext; @@ -512,9 +511,7 @@ public class Component extends SimpleValue implements MetaAttributable { @Override public void registerExportables(Database database) { - if ( ExportableProducer.class.isInstance( subGenerator ) ) { - ( (ExportableProducer) subGenerator ).registerExportables( database ); - } + subGenerator.registerExportables( database ); } }