HHH-18139 clean up one more bifurcation in the Generator framework

removes CustomIdGeneratorCreationContext and IdentifierGeneratorCreator
(which were @Incubating and @Internal, respectively)

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-29 19:28:19 +02:00 committed by Steve Ebersole
parent 411e08a42a
commit 58f555ab39
14 changed files with 78 additions and 104 deletions

View File

@ -511,7 +511,7 @@ public class UuidGenerator
public UuidGenerator(
org.hibernate.annotations.UuidGenerator config,
Member idMember,
CustomIdGeneratorCreationContext creationContext) {
GeneratorCreationContext creationContext) {
this(config, idMember);
}

View File

@ -679,7 +679,7 @@ receive the following arguments:
relying on untyped configuration properties in a Map, etc.
2. The `Member` to which annotation was applied. This allows access to the Java type of the identifier
attribute, etc.
3. `CustomIdGeneratorCreationContext` is a "parameter object" providing access to things often useful
3. `GeneratorCreationContext` is a "parameter object" providing access to things often useful
for identifier generators.

View File

@ -25,7 +25,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* <pre>
* public class CustomSequenceGenerator implements BeforeExecutionGenerator {
* public CustomSequenceGenerator(CustomSequence config, Member annotatedMember,
* CustomIdGeneratorCreationContext context) {
* GeneratorCreationContext context) {
* ...
* }
* ...

View File

@ -79,7 +79,6 @@ import static org.hibernate.internal.CoreLogging.messageLogger;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.qualify;
import static org.hibernate.internal.util.StringHelper.unqualify;
import static org.hibernate.mapping.SimpleValue.DEFAULT_ID_GEN_STRATEGY;
/**
* A binder responsible for interpreting {@link Embeddable} classes and producing
@ -899,7 +898,7 @@ public class EmbeddableBinder {
component.setComponentClassName( inferredData.getClassOrElementType().getName() );
}
component.setCustomInstantiator( customInstantiatorImpl );
final Constructor<?> constructor = resolveInstantiator( inferredData.getClassOrElementType(), context );
final Constructor<?> constructor = resolveInstantiator( inferredData.getClassOrElementType() );
if ( constructor != null ) {
component.setInstantiator( constructor, constructor.getAnnotation( Instantiator.class ).value() );
}
@ -910,11 +909,11 @@ public class EmbeddableBinder {
return component;
}
private static Constructor<?> resolveInstantiator(TypeDetails embeddableClass, MetadataBuildingContext buildingContext) {
return embeddableClass == null ? null : resolveInstantiator( embeddableClass.determineRawClass(), buildingContext );
private static Constructor<?> resolveInstantiator(TypeDetails embeddableClass) {
return embeddableClass == null ? null : resolveInstantiator( embeddableClass.determineRawClass() );
}
private static Constructor<?> resolveInstantiator(ClassDetails embeddableClass, MetadataBuildingContext buildingContext) {
private static Constructor<?> resolveInstantiator(ClassDetails embeddableClass) {
if ( embeddableClass != null ) {
final Constructor<?>[] declaredConstructors = embeddableClass.toJavaClass().getDeclaredConstructors();
Constructor<?> constructor = null;

View File

@ -32,7 +32,6 @@ import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.generator.AnnotationBasedGenerator;
import org.hibernate.generator.Assigned;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.generator.CustomIdGeneratorCreationContext;
import org.hibernate.generator.Generator;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.generator.OnExecutionGenerator;
@ -53,7 +52,6 @@ import org.hibernate.id.enhanced.SingleNamingStrategy;
import org.hibernate.internal.CoreLogging;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.GeneratorCreator;
import org.hibernate.mapping.IdentifierGeneratorCreator;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
@ -92,10 +90,10 @@ public class GeneratorBinder {
private static final Logger LOG = CoreLogging.logger( GeneratorBinder.class );
public static final String ASSIGNED_GENERATOR_NAME = "assigned";
public static final IdentifierGeneratorCreator ASSIGNED_IDENTIFIER_GENERATOR_CREATOR =
new IdentifierGeneratorCreator() {
public static final GeneratorCreator ASSIGNED_IDENTIFIER_GENERATOR_CREATOR =
new GeneratorCreator() {
@Override
public Generator createGenerator(CustomIdGeneratorCreationContext context) {
public Generator createGenerator(GeneratorCreationContext context) {
return new Assigned();
}
@Override
@ -361,8 +359,8 @@ public class GeneratorBinder {
public static Map<String, IdentifierGeneratorDefinition> buildGenerators(
AnnotationTarget annotatedElement,
MetadataBuildingContext context) {
final SourceModelBuildingContext sourceModelContext = context.getMetadataCollector().getSourceModelBuildingContext();
final InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
final SourceModelBuildingContext sourceModelContext = metadataCollector.getSourceModelBuildingContext();
final Map<String, IdentifierGeneratorDefinition> generators = new HashMap<>();
annotatedElement.forEachAnnotationUsage( TableGenerator.class, sourceModelContext, (usage) -> {
@ -432,19 +430,13 @@ public class GeneratorBinder {
final IdentifierGeneratorDefinition.Builder definitionBuilder = new IdentifierGeneratorDefinition.Builder();
if ( generatorAnnotation instanceof TableGenerator tableGenerator ) {
STRATEGY_INTERPRETER.interpretTableGenerator(
tableGenerator,
definitionBuilder
);
STRATEGY_INTERPRETER.interpretTableGenerator( tableGenerator, definitionBuilder );
if ( LOG.isTraceEnabled() ) {
LOG.tracev( "Add table generator with name: {0}", definitionBuilder.getName() );
}
}
else if ( generatorAnnotation instanceof SequenceGenerator sequenceGenerator ) {
STRATEGY_INTERPRETER.interpretSequenceGenerator(
sequenceGenerator,
definitionBuilder
);
STRATEGY_INTERPRETER.interpretSequenceGenerator( sequenceGenerator, definitionBuilder );
if ( LOG.isTraceEnabled() ) {
LOG.tracev( "Add sequence generator with name: {0}", definitionBuilder.getName() );
}
@ -452,9 +444,10 @@ public class GeneratorBinder {
else if ( generatorAnnotation instanceof GenericGenerator genericGenerator ) {
definitionBuilder.setName( genericGenerator.name() );
final Class<? extends Generator> generatorClass = genericGenerator.type();
final String strategy = generatorClass.equals( Generator.class )
? genericGenerator.strategy()
: generatorClass.getName();
final String strategy =
generatorClass.equals( Generator.class )
? genericGenerator.strategy()
: generatorClass.getName();
definitionBuilder.setStrategy( strategy );
definitionBuilder.addParams( extractParameterMap( genericGenerator.parameters() ) );
if ( LOG.isTraceEnabled() ) {
@ -521,19 +514,20 @@ public class GeneratorBinder {
return creationContext -> {
final Generator generator = instantiateGenerator(
annotation,
memberDetails,
annotationType,
beanContainer,
creationContext,
GeneratorCreationContext.class,
generatorClass
generatorClass,
memberDetails,
annotationType
);
callInitialize( annotation, memberDetails, creationContext, generator );
//TODO: callConfigure( creationContext, generator, emptyMap(), identifierValue );
checkVersionGenerationAlways( memberDetails, generator );
return generator;
};
}
static IdentifierGeneratorCreator identifierGeneratorCreator(
static GeneratorCreator identifierGeneratorCreator(
MemberDetails idAttributeMember,
Annotation annotation,
SimpleValue identifierValue,
@ -542,14 +536,13 @@ public class GeneratorBinder {
final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class );
assert idGeneratorType != null;
final Class<? extends Generator> generatorClass = idGeneratorType.value();
checkGeneratorClass( generatorClass );
return creationContext -> {
checkGeneratorClass( generatorClass );
final Generator generator =
instantiateGenerator(
annotation,
beanContainer,
creationContext,
CustomIdGeneratorCreationContext.class,
generatorClass,
idAttributeMember,
annotationType
@ -572,29 +565,26 @@ public class GeneratorBinder {
private static <C> Generator instantiateGenerator(
Annotation annotation,
BeanContainer beanContainer,
C creationContext,
Class<C> creationContextClass,
GeneratorCreationContext creationContext,
Class<? extends Generator> generatorClass,
MemberDetails idAttributeMember,
MemberDetails memberDetails,
Class<? extends Annotation> annotationType) {
if ( beanContainer != null ) {
return instantiateGeneratorAsBean(
annotation,
beanContainer,
creationContext,
creationContextClass,
generatorClass,
idAttributeMember,
memberDetails,
annotationType
);
}
else {
return instantiateGenerator(
annotation,
idAttributeMember,
memberDetails,
annotationType,
creationContext,
creationContextClass,
generatorClass
);
}
@ -608,13 +598,12 @@ public class GeneratorBinder {
* @param beanContainer an optional {@code BeanContainer}
* @param generatorClass a class which implements {@code Generator}
*/
private static <C> Generator instantiateGeneratorAsBean(
private static Generator instantiateGeneratorAsBean(
Annotation annotation,
BeanContainer beanContainer,
C creationContext,
Class<C> creationContextClass,
GeneratorCreationContext creationContext,
Class<? extends Generator> generatorClass,
MemberDetails idAttributeMember,
MemberDetails memberDetails,
Class<? extends Annotation> annotationType) {
return beanContainer.getBean( generatorClass,
new BeanContainer.LifecycleOptions() {
@ -633,10 +622,9 @@ public class GeneratorBinder {
public <B> B produceBeanInstance(Class<B> beanType) {
return (B) instantiateGenerator(
annotation,
idAttributeMember,
memberDetails,
annotationType,
creationContext,
creationContextClass,
generatorClass
);
}
@ -697,16 +685,15 @@ public class GeneratorBinder {
* @param annotation the generator annotation
* @param generatorClass a class which implements {@code Generator}
*/
private static <C, G extends Generator> G instantiateGenerator(
private static <G extends Generator> G instantiateGenerator(
Annotation annotation,
MemberDetails memberDetails,
Class<? extends Annotation> annotationType,
C creationContext,
Class<C> contextClass,
GeneratorCreationContext creationContext,
Class<? extends G> generatorClass) {
try {
try {
return generatorClass.getConstructor( annotationType, Member.class, contextClass )
return generatorClass.getConstructor( annotationType, Member.class, GeneratorCreationContext.class )
.newInstance( annotation, memberDetails.toJavaMember(), creationContext);
}
catch (NoSuchMethodException ignore) {
@ -892,12 +879,12 @@ public class GeneratorBinder {
static BeanContainer beanContainer(MetadataBuildingContext buildingContext) {
final ServiceRegistry serviceRegistry = buildingContext.getBootstrapContext().getServiceRegistry();
return allowExtensionsInCdi( serviceRegistry )
? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer()
: null;
? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer()
: null;
}
/**
* Set up the {@link IdentifierGeneratorCreator} for a case where there is no
* Set up the {@link GeneratorCreator} for a case where there is no
* generator annotation.
*/
private static void setGeneratorCreator(

View File

@ -23,7 +23,7 @@ import java.lang.reflect.Member;
* same as providing a constructor with this signature:
* <pre>
* public GeneratorClass(AnnotationType config, Member idMember,
* CustomIdGeneratorCreationContext creationContext)
* GeneratorCreationContext creationContext)
* </pre>
* <p>
* where {@code GeneratorClass} is the class that implements {@code Generator}, and

View File

@ -1,14 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.generator;
import org.hibernate.Incubating;
@Incubating
@Deprecated(since = "7.0", forRemoval = true)
public interface CustomIdGeneratorCreationContext extends GeneratorCreationContext {
}

View File

@ -13,6 +13,15 @@ import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.service.ServiceRegistry;
/**
* An object passed as a parameter to the constructor or
* {@link AnnotationBasedGenerator#initialize initialize}
* method of a {@link Generator} which provides access to
* certain objects useful for initialization of the
* generator.
*
* @since 6.2
*/
@Incubating
public interface GeneratorCreationContext {
Database getDatabase();

View File

@ -19,7 +19,7 @@ import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.generator.EventType;
import org.hibernate.generator.EventTypeSets;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.generator.CustomIdGeneratorCreationContext;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.type.descriptor.java.UUIDJavaType;
import org.hibernate.type.descriptor.java.UUIDJavaType.ValueTransformer;
@ -102,13 +102,6 @@ public class UuidGenerator implements BeforeExecutionGenerator {
throw new HibernateException( "Unanticipated return type [" + propertyType.getName() + "] for UUID conversion" );
}
public UuidGenerator(
org.hibernate.annotations.UuidGenerator config,
Member idMember,
CustomIdGeneratorCreationContext creationContext) {
this(config, idMember);
}
public UuidGenerator(
org.hibernate.annotations.UuidGenerator config,
Member member,

View File

@ -7,11 +7,31 @@
package org.hibernate.mapping;
import org.hibernate.Internal;
import org.hibernate.generator.Assigned;
import org.hibernate.generator.Generator;
import org.hibernate.generator.GeneratorCreationContext;
/**
* Instantiates a {@link Generator}.
*
* @since 6.2
*
* @author Gavin King
*/
@Internal
@FunctionalInterface
public interface GeneratorCreator {
/**
* Create the generator.
*/
Generator createGenerator(GeneratorCreationContext context);
/**
* Does this object create instances of {@link Assigned}?
*
* @since 7.0
*/
default boolean isAssigned() {
return false;
}
}

View File

@ -1,20 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.mapping;
import org.hibernate.Internal;
import org.hibernate.generator.CustomIdGeneratorCreationContext;
import org.hibernate.generator.Generator;
@Internal
@FunctionalInterface
public interface IdentifierGeneratorCreator {
Generator createGenerator(CustomIdGeneratorCreationContext context);
default boolean isAssigned() {
return false;
}
}

View File

@ -37,7 +37,7 @@ import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.generator.Generator;
import org.hibernate.generator.CustomIdGeneratorCreationContext;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
@ -104,7 +104,7 @@ public abstract class SimpleValue implements KeyValue {
private ConverterDescriptor attributeConverterDescriptor;
private Type type;
private IdentifierGeneratorCreator customIdGeneratorCreator = ASSIGNED_IDENTIFIER_GENERATOR_CREATOR;
private GeneratorCreator customIdGeneratorCreator = ASSIGNED_IDENTIFIER_GENERATOR_CREATOR;
private Generator generator;
public SimpleValue(MetadataBuildingContext buildingContext) {
@ -374,12 +374,12 @@ public abstract class SimpleValue implements KeyValue {
}
@Internal
public void setCustomIdGeneratorCreator(IdentifierGeneratorCreator customIdGeneratorCreator) {
public void setCustomIdGeneratorCreator(GeneratorCreator customIdGeneratorCreator) {
this.customIdGeneratorCreator = customIdGeneratorCreator;
}
@Internal
public IdentifierGeneratorCreator getCustomIdGeneratorCreator() {
public GeneratorCreator getCustomIdGeneratorCreator() {
return customIdGeneratorCreator;
}
@ -1023,7 +1023,7 @@ public abstract class SimpleValue implements KeyValue {
}
}
private class IdGeneratorCreationContext implements CustomIdGeneratorCreationContext {
private class IdGeneratorCreationContext implements GeneratorCreationContext {
private final RootClass rootClass;
public IdGeneratorCreationContext(RootClass rootClass) {

View File

@ -15,8 +15,8 @@ import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.generator.CustomIdGeneratorCreationContext;
/**
* An example custom generator.
@ -34,7 +34,7 @@ public class CustomSequenceGenerator implements IdentifierGenerator {
public CustomSequenceGenerator(
Sequence config,
Member annotatedMember,
CustomIdGeneratorCreationContext context) {
GeneratorCreationContext context) {
//...
//end::identifiers-IdGeneratorType-example[]
final String name = config.name();

View File

@ -8,12 +8,12 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.generator.EventType;
import org.hibernate.generator.Generator;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.generator.OnExecutionGenerator;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.PostInsertIdentityPersister;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.generator.CustomIdGeneratorCreationContext;
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
@ -27,7 +27,7 @@ public class NativeGenerator
private final Generator generator;
public NativeGenerator(NativeId nativeId, Member member, CustomIdGeneratorCreationContext creationContext) {
public NativeGenerator(NativeId nativeId, Member member, GeneratorCreationContext creationContext) {
final String strategy = creationContext.getDatabase().getDialect().getNativeIdentifierGeneratorStrategy();
switch (strategy) {
case "sequence":