HHH-14921 Clarify the ability of IdentifierGenerator to be configured and to register exportables

This commit is contained in:
Yoann Rodière 2021-11-04 10:38:47 +01:00
parent 49d2fccbcb
commit 9d3b3d1c8a
16 changed files with 57 additions and 31 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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

View File

@ -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;

View File

@ -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 <tt>&lt;param&gt;</tt> elements.
* <p>
* 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.
* <p>
* 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.
*

View File

@ -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;

View File

@ -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();

View File

@ -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 <tt>IdentifierGenerator</tt> that requires creation of database objects.
* <br><br>
* All <tt>PersistentIdentifierGenerator</tt>s that also implement
* <tt>Configurable</tt> have access to a special mapping parameter: schema
* All <tt>PersistentIdentifierGenerator</tt>s 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

View File

@ -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

View File

@ -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() );

View File

@ -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";

View File

@ -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;

View File

@ -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,

View File

@ -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()

View File

@ -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 ) {

View File

@ -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 );
}
}