Skip to content

Commit

Permalink
HHH-4309 Check entity class generators to allow overriding generators
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Jan 9, 2025
1 parent 18d576a commit ff5845a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,24 @@ public AbstractEntityIdGeneratorResolver(
@Override
public final void doSecondPass(Map<String, PersistentClass> 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();
case AUTO -> handleAutoStrategy();
}
}

private void handleUuidStrategy() {
GeneratorAnnotationHelper.handleUuidStrategy(
idValue,
idMember,
buildingContext.getMetadataCollector().getClassDetailsRegistry()
.getClassDetails( entityMapping.getClassName() ),
buildingContext
);
}

private void handleSequenceStrategy() {
if ( generatedValue.generator().isBlank() ) {
handleUnnamedSequenceGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,8 +62,9 @@ public class GeneratorAnnotationHelper {
public static <A extends Annotation> A findLocalizedMatch(
AnnotationDescriptor<A> generatorAnnotationType,
MemberDetails idMember,
Function<A,String> nameExtractor,
String matchName,
ClassDetails entityType,
@Nullable Function<A,String> nameExtractor,
@Nullable String matchName,
MetadataBuildingContext context) {
final SourceModelBuildingContext sourceModelContext =
context.getMetadataCollector().getSourceModelBuildingContext();
Expand All @@ -88,7 +90,26 @@ public static <A extends Annotation> 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 ) {
Expand Down Expand Up @@ -296,10 +317,12 @@ public static <A extends Annotation> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ public IdBagIdGeneratorResolverSecondPass(
public void doSecondPass(Map<String, PersistentClass> 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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ protected void handleUnnamedSequenceGenerator() {
final SequenceGenerator localizedMatch = findLocalizedMatch(
JpaAnnotations.SEQUENCE_GENERATOR,
idMember,
buildingContext.getMetadataCollector().getClassDetailsRegistry()
.getClassDetails( entityMapping.getClassName() ),
null,
null,
buildingContext
Expand All @@ -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
Expand Down Expand Up @@ -147,6 +151,8 @@ protected void handleUnnamedTableGenerator() {
final TableGenerator localizedMatch = findLocalizedMatch(
JpaAnnotations.TABLE_GENERATOR,
idMember,
buildingContext.getMetadataCollector().getClassDetailsRegistry()
.getClassDetails( entityMapping.getClassName() ),
null,
null,
buildingContext
Expand All @@ -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
Expand Down Expand Up @@ -229,6 +237,8 @@ protected void handleUnnamedAutoGenerator() {
final SequenceGenerator localizedSequenceMatch = findLocalizedMatch(
JpaAnnotations.SEQUENCE_GENERATOR,
idMember,
buildingContext.getMetadataCollector().getClassDetailsRegistry()
.getClassDetails( entityMapping.getClassName() ),
null,
null,
buildingContext
Expand All @@ -241,6 +251,8 @@ protected void handleUnnamedAutoGenerator() {
final TableGenerator localizedTableMatch = findLocalizedMatch(
JpaAnnotations.TABLE_GENERATOR,
idMember,
buildingContext.getMetadataCollector().getClassDetailsRegistry()
.getClassDetails( entityMapping.getClassName() ),
null,
null,
buildingContext
Expand All @@ -253,6 +265,8 @@ protected void handleUnnamedAutoGenerator() {
final GenericGenerator localizedGenericMatch = findLocalizedMatch(
HibernateAnnotations.GENERIC_GENERATOR,
idMember,
buildingContext.getMetadataCollector().getClassDetailsRegistry()
.getClassDetails( entityMapping.getClassName() ),
null,
null,
buildingContext
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit ff5845a

Please sign in to comment.