HHH-18139 deprecate IdentifierGeneratorFactory

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-18 21:44:14 +02:00 committed by Steve Ebersole
parent 7f3b777cd1
commit 46dd56c715
16 changed files with 67 additions and 107 deletions

View File

@ -2261,11 +2261,7 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector,
// It was done this way in the old code too, so no "regression" here; but // It was done this way in the old code too, so no "regression" here; but
// it could be done better // it could be done better
try { try {
final Generator generator = identifierValueBinding.createGenerator( final Generator generator = identifierValueBinding.createGenerator( dialect, entityBinding );
bootstrapContext.getIdentifierGeneratorFactory(),
dialect,
entityBinding
);
if ( generator instanceof ExportableProducer ) { if ( generator instanceof ExportableProducer ) {
( (ExportableProducer) generator ).registerExportables( getDatabase() ); ( (ExportableProducer) generator ).registerExportables( getDatabase() );

View File

@ -7,7 +7,6 @@
package org.hibernate.boot.internal; package org.hibernate.boot.internal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -63,7 +62,6 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.TimeZoneSupport; import org.hibernate.dialect.TimeZoneSupport;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl; import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.id.factory.IdentifierGeneratorFactory;
@ -71,8 +69,6 @@ import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.log.DeprecationLogger; import org.hibernate.internal.log.DeprecationLogger;
import org.hibernate.internal.util.NullnessHelper; import org.hibernate.internal.util.NullnessHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.CollectionClassification; import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor; import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.function.SqmFunctionRegistry; import org.hibernate.query.sqm.function.SqmFunctionRegistry;
@ -777,7 +773,7 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
return mappingDefaults; return mappingDefaults;
} }
@Override @Override @Deprecated
public IdentifierGeneratorFactory getIdentifierGeneratorFactory() { public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
return identifierGeneratorFactory; return identifierGeneratorFactory;
} }

View File

@ -53,7 +53,7 @@ public abstract class AbstractDelegatingMetadataBuildingOptions implements Metad
return delegate.getMappingDefaults(); return delegate.getMappingDefaults();
} }
@Override @Override @Deprecated
public IdentifierGeneratorFactory getIdentifierGeneratorFactory() { public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
return delegate.getIdentifierGeneratorFactory(); return delegate.getIdentifierGeneratorFactory();
} }

View File

@ -75,6 +75,7 @@ public interface BootstrapContext {
*/ */
MetadataBuildingOptions getMetadataBuildingOptions(); MetadataBuildingOptions getMetadataBuildingOptions();
@Deprecated(since="7.0", forRemoval = true)
default IdentifierGeneratorFactory getIdentifierGeneratorFactory() { default IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
return getMetadataBuildingOptions().getIdentifierGeneratorFactory(); return getMetadataBuildingOptions().getIdentifierGeneratorFactory();
} }

View File

@ -50,7 +50,10 @@ public interface MetadataBuildingOptions {
* The service implementing {@link IdentifierGeneratorFactory}. * The service implementing {@link IdentifierGeneratorFactory}.
* <p> * <p>
* @implNote Almost always a {@link org.hibernate.id.factory.internal.StandardIdentifierGeneratorFactory}. * @implNote Almost always a {@link org.hibernate.id.factory.internal.StandardIdentifierGeneratorFactory}.
*
* @deprecated Use new {@link org.hibernate.generator.Generator} infrastructure
*/ */
@Deprecated(since="7.0", forRemoval = true)
IdentifierGeneratorFactory getIdentifierGeneratorFactory(); IdentifierGeneratorFactory getIdentifierGeneratorFactory();
/** /**

View File

@ -44,8 +44,11 @@ import jakarta.persistence.GenerationType;
* {@link org.hibernate.annotations.IdGeneratorType @IdGeneratorType} meta-annotation. * {@link org.hibernate.annotations.IdGeneratorType @IdGeneratorType} meta-annotation.
* *
* @author Steve Ebersole * @author Steve Ebersole
*
* @deprecated Use new {@link Generator} infrastructure
*/ */
@Incubating //this API is currently in flux @Incubating //this API is currently in flux
@Deprecated(since = "7.0")
public interface IdentifierGeneratorFactory extends Service { public interface IdentifierGeneratorFactory extends Service {
/** /**
* Create an {@link IdentifierGenerator} based on the given details. * Create an {@link IdentifierGenerator} based on the given details.

View File

@ -57,7 +57,10 @@ import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER;
* declared using {@link org.hibernate.annotations.GenericGenerator}. * declared using {@link org.hibernate.annotations.GenericGenerator}.
* *
* @author Steve Ebersole * @author Steve Ebersole
*
* @deprecated Use new {@link Generator} infrastructure
*/ */
@Deprecated(since = "7.0")
public class StandardIdentifierGeneratorFactory public class StandardIdentifierGeneratorFactory
implements IdentifierGeneratorFactory, BeanContainer.LifecycleOptions, Serializable { implements IdentifierGeneratorFactory, BeanContainer.LifecycleOptions, Serializable {

View File

@ -14,5 +14,8 @@
* It is used when id generators are identified by stringly-typed names. * It is used when id generators are identified by stringly-typed names.
* *
* @see org.hibernate.id.factory.IdentifierGeneratorFactory * @see org.hibernate.id.factory.IdentifierGeneratorFactory
*
* @deprecated Use new {@link org.hibernate.generator.Generator} infrastructure
*/ */
@Deprecated(since = "7.0")
package org.hibernate.id.factory; package org.hibernate.id.factory;

View File

@ -7,13 +7,11 @@
package org.hibernate.id.factory.spi; package org.hibernate.id.factory.spi;
import org.hibernate.Incubating; import org.hibernate.Incubating;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.mapping.RootClass;
import org.hibernate.generator.GeneratorCreationContext; import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.mapping.RootClass;
@Incubating @Incubating
public interface CustomIdGeneratorCreationContext extends GeneratorCreationContext { public interface CustomIdGeneratorCreationContext extends GeneratorCreationContext {
IdentifierGeneratorFactory getIdentifierGeneratorFactory();
RootClass getRootClass(); RootClass getRootClass();
// we could add these if it helps integrate old infrastructure // we could add these if it helps integrate old infrastructure

View File

@ -7,5 +7,8 @@
/** /**
* Contains an SPI for id generator factories. * Contains an SPI for id generator factories.
*
* @deprecated Use new {@link org.hibernate.generator.Generator} infrastructure
*/ */
@Deprecated(since = "7.0")
package org.hibernate.id.factory.spi; package org.hibernate.id.factory.spi;

View File

@ -73,7 +73,6 @@ import org.hibernate.generator.Generator;
import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.id.Configurable; import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.integrator.spi.Integrator; import org.hibernate.integrator.spi.Integrator;
import org.hibernate.integrator.spi.IntegratorService; import org.hibernate.integrator.spi.IntegratorService;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
@ -288,7 +287,7 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
try { try {
integrate( bootMetamodel, bootstrapContext, integratorObserver ); integrate( bootMetamodel, bootstrapContext, integratorObserver );
identifierGenerators = createGenerators( jdbcServices, sqlStringGenerationContext, bootMetamodel, bootstrapContext ); identifierGenerators = createGenerators( jdbcServices, sqlStringGenerationContext, bootMetamodel );
bootMetamodel.orderColumns( false ); bootMetamodel.orderColumns( false );
bootMetamodel.validate(); bootMetamodel.validate();
@ -463,26 +462,22 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
private static Map<String, Generator> createGenerators( private static Map<String, Generator> createGenerators(
JdbcServices jdbcServices, JdbcServices jdbcServices,
SqlStringGenerationContext sqlStringGenerationContext, SqlStringGenerationContext sqlStringGenerationContext,
MetadataImplementor bootMetamodel, MetadataImplementor bootMetamodel) {
BootstrapContext bootstrapContext) { final Dialect dialect = jdbcServices.getJdbcEnvironment().getDialect();
final Map<String, Generator> generators = new HashMap<>(); final Map<String, Generator> generators = new HashMap<>();
bootMetamodel.getEntityBindings().stream() for ( PersistentClass model : bootMetamodel.getEntityBindings() ) {
.filter( model -> !model.isInherited() ) if ( !model.isInherited() ) {
.forEach( model -> { final Generator generator = model.getIdentifier().createGenerator( dialect, (RootClass) model );
final Generator generator = model.getIdentifier().createGenerator( if (generator instanceof Configurable) {
bootstrapContext.getIdentifierGeneratorFactory(), final Configurable identifierGenerator = (Configurable) generator;
jdbcServices.getJdbcEnvironment().getDialect(), identifierGenerator.initialize( sqlStringGenerationContext );
(RootClass) model }
); if ( generator.allowAssignedIdentifiers() ) {
if ( generator instanceof Configurable ) { ( (SimpleValue) model.getIdentifier() ).setNullValue( "undefined" );
final Configurable identifierGenerator = (Configurable) generator; }
identifierGenerator.initialize( sqlStringGenerationContext ); generators.put( model.getEntityName(), generator );
} }
if ( generator.allowAssignedIdentifiers() ) { }
( (SimpleValue) model.getIdentifier() ).setNullValue( "undefined" );
}
generators.put( model.getEntityName(), generator );
} );
return generators; return generators;
} }
@ -775,10 +770,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
return sqlStringGenerationContext; return sqlStringGenerationContext;
} }
public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
return null;
}
@Override @Deprecated @Override @Deprecated
public DeserializationResolver<?> getDeserializationResolver() { public DeserializationResolver<?> getDeserializationResolver() {
return () -> (SessionFactoryImplementor) return () -> (SessionFactoryImplementor)

View File

@ -36,7 +36,6 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.CompositeNestedGeneratedValueGenerator; import org.hibernate.id.CompositeNestedGeneratedValueGenerator;
import org.hibernate.id.Configurable; import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerationException; import org.hibernate.id.IdentifierGenerationException;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
@ -669,27 +668,17 @@ public class Component extends SimpleValue implements MetaAttributable, Sortable
} }
@Override @Override
public Generator createGenerator( public Generator createGenerator(Dialect dialect, RootClass rootClass) throws MappingException {
IdentifierGeneratorFactory identifierGeneratorFactory,
Dialect dialect,
RootClass rootClass) throws MappingException {
if ( builtIdentifierGenerator == null ) { if ( builtIdentifierGenerator == null ) {
builtIdentifierGenerator = buildIdentifierGenerator( builtIdentifierGenerator = buildIdentifierGenerator( dialect, rootClass );
identifierGeneratorFactory,
dialect,
rootClass
);
} }
return builtIdentifierGenerator; return builtIdentifierGenerator;
} }
private Generator buildIdentifierGenerator( private Generator buildIdentifierGenerator( Dialect dialect, RootClass rootClass) throws MappingException {
IdentifierGeneratorFactory identifierGeneratorFactory,
Dialect dialect,
RootClass rootClass) throws MappingException {
final boolean hasCustomGenerator = ! DEFAULT_ID_GEN_STRATEGY.equals( getIdentifierGeneratorStrategy() ); final boolean hasCustomGenerator = ! DEFAULT_ID_GEN_STRATEGY.equals( getIdentifierGeneratorStrategy() );
if ( hasCustomGenerator ) { if ( hasCustomGenerator ) {
return super.createGenerator( identifierGeneratorFactory, dialect, rootClass ); return super.createGenerator( dialect, rootClass );
} }
final Class<?> entityClass = rootClass.getMappedClass(); final Class<?> entityClass = rootClass.getMappedClass();
@ -724,7 +713,7 @@ public class Component extends SimpleValue implements MetaAttributable, Sortable
// skip any 'assigned' generators, they would have been handled by // skip any 'assigned' generators, they would have been handled by
// the StandardGenerationContextLocator // the StandardGenerationContextLocator
generator.addGeneratedValuePlan( new ValueGenerationPlan( generator.addGeneratedValuePlan( new ValueGenerationPlan(
value.createGenerator( identifierGeneratorFactory, dialect, rootClass ), value.createGenerator( dialect, rootClass ),
getType().isMutable() ? injector( property, attributeDeclarer ) : null, getType().isMutable() ? injector( property, attributeDeclarer ) : null,
i i
) ); ) );

View File

@ -29,13 +29,10 @@ public interface KeyValue extends Value {
boolean isUpdateable(); boolean isUpdateable();
Generator createGenerator( Generator createGenerator(Dialect dialect, RootClass rootClass);
IdentifierGeneratorFactory identifierGeneratorFactory,
Dialect dialect,
RootClass rootClass);
/** /**
* @deprecated Use {@link #createGenerator(IdentifierGeneratorFactory, Dialect, RootClass)} instead. * @deprecated Use {@link #createGenerator(Dialect, RootClass)} instead.
* No longer used except in legacy tests. * No longer used except in legacy tests.
* *
* @return {@code null} if the {@code Generator} returned by {@link #createGenerator} is not an instance * @return {@code null} if the {@code Generator} returned by {@link #createGenerator} is not an instance
@ -48,12 +45,12 @@ public interface KeyValue extends Value {
String defaultCatalog, String defaultCatalog,
String defaultSchema, String defaultSchema,
RootClass rootClass) { RootClass rootClass) {
final Generator generator = createGenerator( identifierGeneratorFactory, dialect, rootClass ); final Generator generator = createGenerator( dialect, rootClass );
return generator instanceof IdentifierGenerator ? (IdentifierGenerator) generator : null; return generator instanceof IdentifierGenerator ? (IdentifierGenerator) generator : null;
} }
/** /**
* @deprecated Use {@link #createGenerator(IdentifierGeneratorFactory, Dialect, RootClass)} instead. * @deprecated Use {@link #createGenerator(Dialect, RootClass)} instead.
* No longer used except in legacy tests. * No longer used except in legacy tests.
* *
* @return {@code null} if the {@code Generator} returned by {@link #createGenerator} is not an instance * @return {@code null} if the {@code Generator} returned by {@link #createGenerator} is not an instance
@ -64,7 +61,7 @@ public interface KeyValue extends Value {
IdentifierGeneratorFactory identifierGeneratorFactory, IdentifierGeneratorFactory identifierGeneratorFactory,
Dialect dialect, Dialect dialect,
RootClass rootClass) { RootClass rootClass) {
final Generator generator = createGenerator( identifierGeneratorFactory, dialect, rootClass ); final Generator generator = createGenerator( dialect, rootClass );
return generator instanceof IdentifierGenerator ? (IdentifierGenerator) generator : null; return generator instanceof IdentifierGenerator ? (IdentifierGenerator) generator : null;
} }
} }

View File

@ -40,7 +40,6 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping; import org.hibernate.engine.spi.Mapping;
import org.hibernate.generator.Generator; import org.hibernate.generator.Generator;
import org.hibernate.id.IdentityGenerator; import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext; import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
@ -391,14 +390,11 @@ public abstract class SimpleValue implements KeyValue {
} }
@Override @Override
public Generator createGenerator( public Generator createGenerator(Dialect dialect, RootClass rootClass) throws MappingException {
IdentifierGeneratorFactory identifierGeneratorFactory,
Dialect dialect,
RootClass rootClass) throws MappingException {
if ( generator == null ) { if ( generator == null ) {
if ( customIdGeneratorCreator != null ) { if ( customIdGeneratorCreator != null ) {
generator = customIdGeneratorCreator.createGenerator( generator = customIdGeneratorCreator.createGenerator(
new IdGeneratorCreationContext( identifierGeneratorFactory, null, null, rootClass ) new IdGeneratorCreationContext( null, null, rootClass )
); );
} }
else { else {
@ -1104,23 +1100,16 @@ public abstract class SimpleValue implements KeyValue {
} }
private class IdGeneratorCreationContext implements CustomIdGeneratorCreationContext { private class IdGeneratorCreationContext implements CustomIdGeneratorCreationContext {
private final IdentifierGeneratorFactory identifierGeneratorFactory;
private final String defaultCatalog; private final String defaultCatalog;
private final String defaultSchema; private final String defaultSchema;
private final RootClass rootClass; private final RootClass rootClass;
public IdGeneratorCreationContext(IdentifierGeneratorFactory identifierGeneratorFactory, String defaultCatalog, String defaultSchema, RootClass rootClass) { public IdGeneratorCreationContext(String defaultCatalog, String defaultSchema, RootClass rootClass) {
this.identifierGeneratorFactory = identifierGeneratorFactory;
this.defaultCatalog = defaultCatalog; this.defaultCatalog = defaultCatalog;
this.defaultSchema = defaultSchema; this.defaultSchema = defaultSchema;
this.rootClass = rootClass; this.rootClass = rootClass;
} }
@Override
public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
return identifierGeneratorFactory;
}
@Override @Override
public Database getDatabase() { public Database getDatabase() {
return buildingContext.getMetadataCollector().getDatabase(); return buildingContext.getMetadataCollector().getDatabase();

View File

@ -639,11 +639,7 @@ public abstract class AbstractCollectionPersister
} }
private BeforeExecutionGenerator createGenerator(RuntimeModelCreationContext context, IdentifierCollection collection) { private BeforeExecutionGenerator createGenerator(RuntimeModelCreationContext context, IdentifierCollection collection) {
final Generator generator = collection.getIdentifier().createGenerator( final Generator generator = collection.getIdentifier().createGenerator( context.getDialect(), null );
context.getBootstrapContext().getIdentifierGeneratorFactory(),
context.getDialect(),
null
);
if ( generator.generatedOnExecution() ) { if ( generator.generatedOnExecution() ) {
throw new MappingException("must be an BeforeExecutionGenerator"); //TODO fix message throw new MappingException("must be an BeforeExecutionGenerator"); //TODO fix message
} }

View File

@ -10,8 +10,9 @@ import org.hibernate.generator.EventType;
import org.hibernate.generator.Generator; import org.hibernate.generator.Generator;
import org.hibernate.generator.OnExecutionGenerator; import org.hibernate.generator.OnExecutionGenerator;
import org.hibernate.id.Configurable; import org.hibernate.id.Configurable;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.PostInsertIdentityPersister; import org.hibernate.id.PostInsertIdentityPersister;
import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext; import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext;
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate; import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -24,16 +25,20 @@ import java.util.Properties;
public class NativeGenerator public class NativeGenerator
implements OnExecutionGenerator, BeforeExecutionGenerator, Configurable, ExportableProducer { implements OnExecutionGenerator, BeforeExecutionGenerator, Configurable, ExportableProducer {
private final IdentifierGeneratorFactory factory; private final Generator generator;
private final String strategy;
private Generator generator;
public NativeGenerator(NativeId nativeId, Member member, CustomIdGeneratorCreationContext creationContext) { public NativeGenerator(NativeId nativeId, Member member, CustomIdGeneratorCreationContext creationContext) {
factory = creationContext.getIdentifierGeneratorFactory(); final String strategy = creationContext.getDatabase().getDialect().getNativeIdentifierGeneratorStrategy();
strategy = creationContext.getDatabase().getDialect().getNativeIdentifierGeneratorStrategy(); switch (strategy) {
if ( "identity".equals(strategy) ) { case "sequence":
creationContext.getProperty().getValue().getColumns().get(0).setIdentity(true); generator = new SequenceStyleGenerator();
break;
case "identity":
creationContext.getProperty().getValue().getColumns().get(0).setIdentity(true);
generator = new IdentityGenerator();
break;
default:
throw new IllegalArgumentException();
} }
} }
@ -49,22 +54,9 @@ public class NativeGenerator
@Override @Override
public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry) { public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry) {
generator = factory.createIdentifierGenerator(strategy, type, parameters); if ( generator instanceof Configurable ) {
//TODO: should use this instead of the deprecated method, but see HHH-18135 ((Configurable) generator).configure( type, parameters, serviceRegistry );
// GenerationType generationType; }
// switch (strategy) {
// case "identity":
// generationType = GenerationType.IDENTITY;
// break;
// case "sequence":
// generationType = GenerationType.SEQUENCE;
// break;
// default:
// throw new AssertionFailure("unrecognized strategy");
// }
// generator =
// factory.createIdentifierGenerator( generationType, strategy, strategy, type.getJavaTypeDescriptor(),
// parameters, (a, b) -> null );
} }
@Override @Override