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 4ba772b72d
commit 673f03304f
3 changed files with 44 additions and 2 deletions

View File

@ -388,8 +388,16 @@ public class EmbeddableBinder {
final XProperty property = propertyAnnotatedElement.getProperty();
if ( property.isAnnotationPresent( GeneratedValue.class ) ) {
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" );
}
}
}
if ( compositeUserType != null ) {

View File

@ -67,6 +67,7 @@ import org.hibernate.annotations.Where;
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.ConstraintMode;
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 @@ public class EntityBinder {
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

View File

@ -76,3 +76,27 @@ Finally, the `SelfRenderingFunctionSqlAstExpression.getRenderer()` method was de
== Supported Dialects
Please also do have a look at the link:{versionDocBase}/dialect/dialect.html[Supported Dialects] document to check for potential upgrades to the minimum version of the databases underpinning the dialects that Hibernate currently supports.
[[GeneratedValues]]
== Generated Values
Previous versions of Hibernate silently ignored annotations `@GeneratedValue` applied to non identifier fields.
E.g.
```
@Entity
class Entity{
@Id
Integer id,
@GeneratedValue // Ignored in Hibernate versions < 6.4, since 6.4 an AnnotationException is thrown
String name;
}
```
From 6.4 this mapping is not allowed anymore and an `AnnotationException` will be raised.