From 090062220c952dddf432135e0e48ff391289470d Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 19 Jan 2024 11:59:48 +0100 Subject: [PATCH] HHH-17653 - Error in generating schema when @Generator annotation is applied to a non id embeddable property --- .../boot/model/internal/EmbeddableBinder.java | 10 +++++++++- .../hibernate/boot/model/internal/EntityBinder.java | 12 +++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java index 8781b94be9..674adad8f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java @@ -388,7 +388,15 @@ static Component fillEmbeddable( final XProperty property = propertyAnnotatedElement.getProperty(); if ( property.isAnnotationPresent( GeneratedValue.class ) ) { - processGeneratedId( context, component, property ); + if ( isIdClass || subholder.isOrWithinEmbeddedId() ) { + processGeneratedId( context, component, property ); + } + else { + throw new AnnotationException( + "Property '" + property.getName() + "' of '" + + getPath( propertyHolder, inferredData ) + + "' is annotated '@GeneratedValue' but is not part of an identifier" ); + } } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java index 527a30c39c..d6e913d48c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java @@ -67,6 +67,7 @@ import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XAnnotatedElement; import org.hibernate.annotations.common.reflection.XClass; +import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.binder.TypeBinder; import org.hibernate.boot.model.IdentifierGeneratorDefinition; import org.hibernate.boot.model.NamedEntityGraphDefinition; @@ -116,6 +117,7 @@ import jakarta.persistence.DiscriminatorColumn; import jakarta.persistence.DiscriminatorValue; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; import jakarta.persistence.IdClass; import jakarta.persistence.Inheritance; import jakarta.persistence.InheritanceType; @@ -1015,14 +1017,22 @@ private void processIdPropertiesIfNotAlready( for ( PropertyData propertyAnnotatedElement : elementsToProcess.getElements() ) { final String propertyName = propertyAnnotatedElement.getPropertyName(); if ( !idPropertiesIfIdClass.contains( propertyName ) ) { + final XProperty property = propertyAnnotatedElement.getProperty(); + boolean hasIdAnnotation = hasIdAnnotation( property ); if ( !idPropertiesIfIdClass.isEmpty() && !isIgnoreIdAnnotations() - && hasIdAnnotation( propertyAnnotatedElement.getProperty() ) ) { + && hasIdAnnotation ) { missingEntityProperties.add( propertyName ); } else { boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE && inheritanceState.hasParents(); + if ( !hasIdAnnotation && property.isAnnotationPresent( GeneratedValue.class ) ) { + throw new AnnotationException( + "Property '" + + BinderHelper.getPath( propertyHolder, propertyAnnotatedElement ) + + "' is annotated @GeneratedValue but is not part of an identifier" ); + } processElementAnnotations( propertyHolder, subclassAndSingleTableStrategy