HHH-17653 - Error in generating schema when @Generator annotation is applied to a non id embeddable property

This commit is contained in:
Andrea Boriero 2024-01-19 11:59:48 +01:00 committed by Christian Beikov
parent 91b88bb0ad
commit 090062220c
2 changed files with 20 additions and 2 deletions

View File

@ -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" );
}
}
}

View File

@ -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