light refactoring to GeneratorBinder

This commit is contained in:
Gavin King 2024-11-22 01:45:46 +01:00
parent 2f7052c0ce
commit 800a3f0738
1 changed files with 64 additions and 98 deletions

View File

@ -42,7 +42,6 @@ import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.id.uuid.UuidValueGenerator;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.GeneratorCreator;
import org.hibernate.mapping.KeyValue;
import org.hibernate.mapping.PersistentClass;
@ -70,9 +69,9 @@ import static org.hibernate.boot.model.internal.GeneratorParameters.interpretTab
import static org.hibernate.boot.model.internal.GeneratorStrategies.generatorClass;
import static org.hibernate.id.IdentifierGenerator.GENERATOR_NAME;
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import static org.hibernate.internal.util.StringHelper.qualify;
import static org.hibernate.internal.util.collections.CollectionHelper.combineUntyped;
import static org.hibernate.resource.beans.internal.Helper.allowExtensionsInCdi;
/**
* Responsible for configuring and instantiating {@link Generator}s.
@ -120,32 +119,13 @@ public class GeneratorBinder {
if ( generatedValue != null ) {
// The mapping used @GeneratedValue but specified no name. This is a special case added in JPA 3.2.
// Look for a matching "implied generator" based on the GenerationType
final GenerationType strategy = generatedValue.strategy();
final String strategyGeneratorClassName = correspondingGeneratorName( strategy );
final IdentifierGeneratorDefinition impliedGenerator = determineImpliedGenerator(
strategy,
strategyGeneratorClassName,
localGenerators
);
final IdentifierGeneratorDefinition impliedGenerator =
determineImpliedGenerator( strategy, strategyGeneratorClassName, localGenerators );
if ( impliedGenerator != null ) {
configuration.putAll( impliedGenerator.getParameters() );
final BeanContainer beanContainer = beanContainer( context );
identifierValue.setCustomIdGeneratorCreator( creationContext -> {
final Generator identifierGenerator = instantiateGenerator(
beanContainer,
generatorClass( strategyGeneratorClassName, identifierValue )
);
callConfigure( creationContext, identifierGenerator, configuration, identifierValue );
if ( identifierGenerator instanceof IdentityGenerator) {
identifierValue.setColumnToIdentity();
}
return identifierGenerator;
} );
instantiateGeneratorBean( identifierValue, strategyGeneratorClassName, configuration, context );
return;
}
}
@ -171,7 +151,8 @@ public class GeneratorBinder {
}
if ( localGenerators.size() == 1 ) {
final IdentifierGeneratorDefinition generatorDefinition = localGenerators.entrySet().iterator().next().getValue();
final IdentifierGeneratorDefinition generatorDefinition =
localGenerators.values().iterator().next();
// NOTE : a little bit of a special rule here for the case of just one -
// we consider it a match, based on strategy, if the strategy is AUTO or matches...
if ( strategy == AUTO
@ -181,13 +162,13 @@ public class GeneratorBinder {
}
IdentifierGeneratorDefinition matching = null;
for ( Map.Entry<String, ? extends IdentifierGeneratorDefinition> localGeneratorEntry : localGenerators.entrySet() ) {
if ( isImpliedGenerator( strategy, strategyGeneratorClassName, localGeneratorEntry.getValue() ) ) {
for ( IdentifierGeneratorDefinition localGenerator : localGenerators.values() ) {
if ( isImpliedGenerator( strategy, strategyGeneratorClassName, localGenerator ) ) {
if ( matching != null ) {
// we found multiple matching generators
return null;
}
matching = localGeneratorEntry.getValue();
matching = localGenerator;
}
}
return matching;
@ -259,20 +240,20 @@ public class GeneratorBinder {
if ( globalDefinition != null ) {
return globalDefinition;
}
LOG.debugf( "Could not resolve explicit IdentifierGeneratorDefinition - using implicit interpretation (%s)", name );
final GeneratedValue generatedValue = idAttributeMember.getDirectAnnotationUsage( GeneratedValue.class );
if ( generatedValue == null ) {
throw new AssertionFailure( "No @GeneratedValue annotation" );
else {
LOG.debugf( "Could not resolve explicit IdentifierGeneratorDefinition - using implicit interpretation (%s)",
name );
final GeneratedValue generatedValue = idAttributeMember.getDirectAnnotationUsage( GeneratedValue.class );
if ( generatedValue == null ) {
throw new AssertionFailure( "No @GeneratedValue annotation" );
}
return IdentifierGeneratorDefinition.createImplicit(
name,
idAttributeMember.getType(),
generatedValue.generator(),
interpretGenerationType( generatedValue )
);
}
return IdentifierGeneratorDefinition.createImplicit(
name,
idAttributeMember.getType(),
generatedValue.generator(),
interpretGenerationType( generatedValue )
);
}
private static GenerationType interpretGenerationType(GeneratedValue generatedValueAnn) {
@ -285,43 +266,35 @@ public class GeneratorBinder {
AnnotationTarget annotatedElement,
Consumer<IdentifierGeneratorDefinition> consumer,
MetadataBuildingContext context) {
final InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
final SourceModelBuildingContext sourceModelContext = metadataCollector.getSourceModelBuildingContext();
final SourceModelBuildingContext sourceModelContext =
context.getMetadataCollector().getSourceModelBuildingContext();
annotatedElement.forEachAnnotationUsage( TableGenerator.class, sourceModelContext, (usage) -> {
final IdentifierGeneratorDefinition idGenerator = buildTableIdGenerator( usage );
consumer.accept( idGenerator );
} );
annotatedElement.forEachAnnotationUsage( TableGenerator.class, sourceModelContext,
usage -> consumer.accept( buildTableIdGenerator( usage ) ) );
annotatedElement.forEachAnnotationUsage( SequenceGenerator.class, sourceModelContext, (usage) -> {
final IdentifierGeneratorDefinition idGenerator = buildSequenceIdGenerator( usage );
consumer.accept( idGenerator );
} );
annotatedElement.forEachAnnotationUsage( SequenceGenerator.class, sourceModelContext,
usage -> consumer.accept( buildSequenceIdGenerator( usage ) ) );
annotatedElement.forEachAnnotationUsage( GenericGenerator.class, sourceModelContext, (usage) -> {
final IdentifierGeneratorDefinition idGenerator = buildIdGenerator( usage );
consumer.accept( idGenerator );
} );
annotatedElement.forEachAnnotationUsage( GenericGenerator.class, sourceModelContext,
usage -> consumer.accept( buildIdGenerator( usage ) ) );
}
public static void registerGlobalGenerators(
AnnotationTarget annotatedElement,
MetadataBuildingContext context) {
if ( !context.getBootstrapContext().getJpaCompliance().isGlobalGeneratorScopeEnabled() ) {
return;
if ( context.getBootstrapContext().getJpaCompliance().isGlobalGeneratorScopeEnabled() ) {
final InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
visitIdGeneratorDefinitions(
annotatedElement,
definition -> {
if ( !definition.getName().isEmpty() ) {
metadataCollector.addIdentifierGenerator( definition );
}
},
context
);
}
final InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
visitIdGeneratorDefinitions(
annotatedElement,
(definition) -> {
if ( !definition.getName().isEmpty() ) {
metadataCollector.addIdentifierGenerator( definition );
}
},
context
);
}
private static IdentifierGeneratorDefinition buildIdGenerator(GenericGenerator generatorAnnotation) {
@ -580,12 +553,9 @@ public class GeneratorBinder {
public static <T extends Generator> T instantiateGenerator(
BeanContainer beanContainer,
Class<T> generatorClass) {
if ( beanContainer != null ) {
return instantiateGeneratorAsBean( beanContainer, generatorClass );
}
else {
return instantiateGeneratorViaDefaultConstructor( generatorClass );
}
return beanContainer != null
? instantiateGeneratorAsBean( beanContainer, generatorClass )
: instantiateGeneratorViaDefaultConstructor( generatorClass );
}
/**
@ -712,19 +682,7 @@ public class GeneratorBinder {
Map<String, Object> configuration,
MetadataBuildingContext context) {
configuration.putAll( defaultedGenerator.getParameters() );
final BeanContainer beanContainer = beanContainer( context );
idValue.setCustomIdGeneratorCreator( creationContext -> {
final Generator identifierGenerator = instantiateGenerator(
beanContainer,
generatorClass( defaultedGenerator.getStrategy(), idValue )
);
callConfigure( creationContext, identifierGenerator, configuration, idValue );
if ( identifierGenerator instanceof IdentityGenerator) {
idValue.setColumnToIdentity();
}
return identifierGenerator;
} );
instantiateGeneratorBean( idValue, defaultedGenerator.getStrategy(), configuration, context );
}
@ -802,19 +760,27 @@ public class GeneratorBinder {
identifierValue.setCustomIdGeneratorCreator( ASSIGNED_IDENTIFIER_GENERATOR_CREATOR );
}
else {
final BeanContainer beanContainer = beanContainer( context );
identifierValue.setCustomIdGeneratorCreator( creationContext -> {
final Generator identifierGenerator =
instantiateGenerator( beanContainer, generatorClass( generatorStrategy, identifierValue ) );
callConfigure( creationContext, identifierGenerator, configuration, identifierValue );
if ( identifierGenerator instanceof IdentityGenerator) {
identifierValue.setColumnToIdentity();
}
return identifierGenerator;
} );
instantiateGeneratorBean( identifierValue, generatorStrategy, configuration, context );
}
}
private static void instantiateGeneratorBean(
SimpleValue identifierValue,
String generatorStrategy,
Map<String, Object> configuration,
MetadataBuildingContext context) {
final BeanContainer beanContainer = beanContainer( context );
identifierValue.setCustomIdGeneratorCreator( creationContext -> {
final Generator identifierGenerator =
instantiateGenerator( beanContainer, generatorClass( generatorStrategy, identifierValue ) );
callConfigure( creationContext, identifierGenerator, configuration, identifierValue );
if ( identifierGenerator instanceof IdentityGenerator) {
identifierValue.setColumnToIdentity();
}
return identifierGenerator;
} );
}
/**
* Set up the id generator by considering all annotations of the identifier
* field, including {@linkplain IdGeneratorType id generator annotations},
@ -890,7 +856,7 @@ public class GeneratorBinder {
}
public static void applyIfNotEmpty(String name, String value, BiConsumer<String,String> consumer) {
if ( StringHelper.isNotEmpty( value ) ) {
if ( isNotEmpty( value ) ) {
consumer.accept( name, value );
}
}