Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
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;
Expand Down Expand Up @@ -70,9 +69,9 @@
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.
Expand Down Expand Up @@ -120,32 +119,13 @@ public static void makeIdGenerator(
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;
}
}
Expand All @@ -171,7 +151,8 @@ private static IdentifierGeneratorDefinition determineImpliedGenerator(
}

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
Expand All @@ -181,13 +162,13 @@ private static IdentifierGeneratorDefinition determineImpliedGenerator(
}

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;
Expand Down Expand Up @@ -259,20 +240,20 @@ private static IdentifierGeneratorDefinition makeIdentifierGeneratorDefinition(
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) {
Expand All @@ -285,43 +266,35 @@ public static void visitIdGeneratorDefinitions(
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) {
Expand Down Expand Up @@ -580,12 +553,9 @@ private static <G extends Generator> G instantiateGenerator(
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 );
}

/**
Expand Down Expand Up @@ -712,19 +682,7 @@ public static void createGeneratorFrom(
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 );
}


Expand Down Expand Up @@ -802,19 +760,27 @@ private static void setGeneratorCreator(
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},
Expand Down Expand Up @@ -890,7 +856,7 @@ static GeneratorCreator createValueGeneratorFromAnnotations(
}

public static void applyIfNotEmpty(String name, String value, BiConsumer<String,String> consumer) {
if ( StringHelper.isNotEmpty( value ) ) {
if ( isNotEmpty( value ) ) {
consumer.accept( name, value );
}
}
Expand Down
Loading