diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AbstractEntityIdGeneratorResolver.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AbstractEntityIdGeneratorResolver.java index 319a78162f53..17455c65c480 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AbstractEntityIdGeneratorResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AbstractEntityIdGeneratorResolver.java @@ -64,7 +64,7 @@ public AbstractEntityIdGeneratorResolver( @Override public final void doSecondPass(Map persistentClasses) throws MappingException { switch ( generatedValue.strategy() ) { - case UUID -> GeneratorAnnotationHelper.handleUuidStrategy( idValue, idMember, buildingContext ); + case UUID -> handleUuidStrategy(); case IDENTITY -> GeneratorAnnotationHelper.handleIdentityStrategy( idValue ); case SEQUENCE -> handleSequenceStrategy(); case TABLE -> handleTableStrategy(); @@ -72,6 +72,16 @@ public final void doSecondPass(Map persistentClasses) t } } + private void handleUuidStrategy() { + GeneratorAnnotationHelper.handleUuidStrategy( + idValue, + idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), + buildingContext + ); + } + private void handleSequenceStrategy() { if ( generatedValue.generator().isBlank() ) { handleUnnamedSequenceGenerator(); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorAnnotationHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorAnnotationHelper.java index 970ec96ec494..34944807ee16 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorAnnotationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorAnnotationHelper.java @@ -13,6 +13,7 @@ import java.util.function.Consumer; import java.util.function.Function; +import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.IdGeneratorType; import org.hibernate.annotations.Parameter; @@ -61,8 +62,9 @@ public class GeneratorAnnotationHelper { public static A findLocalizedMatch( AnnotationDescriptor generatorAnnotationType, MemberDetails idMember, - Function nameExtractor, - String matchName, + ClassDetails entityType, + @Nullable Function nameExtractor, + @Nullable String matchName, MetadataBuildingContext context) { final SourceModelBuildingContext sourceModelContext = context.getMetadataCollector().getSourceModelBuildingContext(); @@ -88,7 +90,26 @@ public static A findLocalizedMatch( } } - // next, on the class + // next, on the entity class + for ( A generatorAnnotation : + entityType.getRepeatedAnnotationUsages( generatorAnnotationType, sourceModelContext ) ) { + if ( nameExtractor != null ) { + final String registrationName = nameExtractor.apply( generatorAnnotation ); + if ( registrationName.isEmpty() ) { + if ( possibleMatch == null ) { + possibleMatch = generatorAnnotation; + } + } + else if ( registrationName.equals( matchName ) ) { + return generatorAnnotation; + } + } + else { + return generatorAnnotation; + } + } + + // next, on the declaring class for ( A generatorAnnotation: idMember.getDeclaringType().getRepeatedAnnotationUsages( generatorAnnotationType, sourceModelContext ) ) { if ( nameExtractor != null ) { @@ -296,10 +317,12 @@ public static void prepareForUse( public static void handleUuidStrategy( SimpleValue idValue, MemberDetails idMember, + ClassDetails entityClass, MetadataBuildingContext context) { final org.hibernate.annotations.UuidGenerator generatorConfig = findLocalizedMatch( HibernateAnnotations.UUID_GENERATOR, idMember, + entityClass, null, null, context diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdBagIdGeneratorResolverSecondPass.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdBagIdGeneratorResolverSecondPass.java index 835c1d5177aa..082e44f3a91c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdBagIdGeneratorResolverSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdBagIdGeneratorResolverSecondPass.java @@ -71,7 +71,13 @@ public IdBagIdGeneratorResolverSecondPass( public void doSecondPass(Map idGeneratorDefinitionMap) throws MappingException { final GeneratedValue generatedValue = idBagMember.getDirectAnnotationUsage( GeneratedValue.class ); switch ( generatedValue.strategy() ) { - case UUID -> handleUuidStrategy( idValue, idBagMember, buildingContext ); + case UUID -> handleUuidStrategy( + idValue, + idBagMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), + buildingContext + ); case IDENTITY -> handleIdentityStrategy( idValue ); case SEQUENCE -> handleSequenceStrategy( generatorName, @@ -121,6 +127,8 @@ private void handleTableStrategy( final TableGenerator localizedTableMatch = findLocalizedMatch( JpaAnnotations.TABLE_GENERATOR, idBagMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), TableGenerator::name, generatorName, buildingContext @@ -164,6 +172,8 @@ private void handleSequenceStrategy( final SequenceGenerator localizedSequencedMatch = findLocalizedMatch( JpaAnnotations.SEQUENCE_GENERATOR, idBagMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), SequenceGenerator::name, generatorName, buildingContext @@ -235,6 +245,8 @@ private void handleAutoStrategy( final SequenceGenerator localizedSequencedMatch = findLocalizedMatch( JpaAnnotations.SEQUENCE_GENERATOR, idBagMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), SequenceGenerator::name, generatorName, buildingContext @@ -247,6 +259,8 @@ private void handleAutoStrategy( final TableGenerator localizedTableMatch = findLocalizedMatch( JpaAnnotations.TABLE_GENERATOR, idBagMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), TableGenerator::name, generatorName, buildingContext diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdGeneratorResolverSecondPass.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdGeneratorResolverSecondPass.java index adb6cbe47607..e79f2f8927b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdGeneratorResolverSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdGeneratorResolverSecondPass.java @@ -58,6 +58,8 @@ protected void handleUnnamedSequenceGenerator() { final SequenceGenerator localizedMatch = findLocalizedMatch( JpaAnnotations.SEQUENCE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), null, null, buildingContext @@ -77,6 +79,8 @@ protected void handleNamedSequenceGenerator() { final SequenceGenerator localizedMatch = findLocalizedMatch( JpaAnnotations.SEQUENCE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), SequenceGenerator::name, generator, buildingContext @@ -147,6 +151,8 @@ protected void handleUnnamedTableGenerator() { final TableGenerator localizedMatch = findLocalizedMatch( JpaAnnotations.TABLE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), null, null, buildingContext @@ -161,6 +167,8 @@ protected void handleNamedTableGenerator() { final TableGenerator localizedTableMatch = findLocalizedMatch( JpaAnnotations.TABLE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), TableGenerator::name, generator, buildingContext @@ -229,6 +237,8 @@ protected void handleUnnamedAutoGenerator() { final SequenceGenerator localizedSequenceMatch = findLocalizedMatch( JpaAnnotations.SEQUENCE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), null, null, buildingContext @@ -241,6 +251,8 @@ protected void handleUnnamedAutoGenerator() { final TableGenerator localizedTableMatch = findLocalizedMatch( JpaAnnotations.TABLE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), null, null, buildingContext @@ -253,6 +265,8 @@ protected void handleUnnamedAutoGenerator() { final GenericGenerator localizedGenericMatch = findLocalizedMatch( HibernateAnnotations.GENERIC_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), null, null, buildingContext @@ -274,7 +288,13 @@ protected void handleUnnamedAutoGenerator() { if ( idMember.getType().isImplementor( UUID.class ) || idMember.getType().isImplementor( String.class ) ) { - GeneratorAnnotationHelper.handleUuidStrategy( idValue, idMember, buildingContext ); + GeneratorAnnotationHelper.handleUuidStrategy( + idValue, + idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), + buildingContext + ); return; } @@ -313,7 +333,13 @@ protected void handleNamedAutoGenerator() { if ( idMember.getType().isImplementor( UUID.class ) || idMember.getType().isImplementor( String.class ) ) { - GeneratorAnnotationHelper.handleUuidStrategy( idValue, idMember, buildingContext ); + GeneratorAnnotationHelper.handleUuidStrategy( + idValue, + idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), + buildingContext + ); return; } @@ -331,6 +357,8 @@ private boolean handleAsLocalAutoGenerator() { final SequenceGenerator localizedSequenceMatch = findLocalizedMatch( JpaAnnotations.SEQUENCE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), SequenceGenerator::name, generator, buildingContext @@ -343,6 +371,8 @@ private boolean handleAsLocalAutoGenerator() { final TableGenerator localizedTableMatch = findLocalizedMatch( JpaAnnotations.TABLE_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), TableGenerator::name, generator, buildingContext @@ -355,6 +385,8 @@ private boolean handleAsLocalAutoGenerator() { final GenericGenerator localizedGenericMatch = findLocalizedMatch( HibernateAnnotations.GENERIC_GENERATOR, idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), GenericGenerator::name, generator, buildingContext diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/StrictIdGeneratorResolverSecondPass.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/StrictIdGeneratorResolverSecondPass.java index b79d98e376be..d09e397c1b8f 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/StrictIdGeneratorResolverSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/StrictIdGeneratorResolverSecondPass.java @@ -220,7 +220,13 @@ private void handleAutoGenerator(String globalRegistrationName) { // Implicit handling of UUID generation if ( idMember.getType().isImplementor( UUID.class ) || idMember.getType().isImplementor( String.class ) ) { - handleUuidStrategy( idValue, idMember, buildingContext ); + handleUuidStrategy( + idValue, + idMember, + buildingContext.getMetadataCollector().getClassDetailsRegistry() + .getClassDetails( entityMapping.getClassName() ), + buildingContext + ); return; }