Skip to content

Commit fc2b1b9

Browse files
committed
HHH-18626 fix error for @id annotation in @embeddable class
1 parent e9bf523 commit fc2b1b9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,12 @@ private static void buildProperty(
781781
propertyBinder.setInheritanceStatePerClass( inheritanceStatePerClass );
782782
propertyBinder.setId( !entityBinder.isIgnoreIdAnnotations() && hasIdAnnotation( property ) );
783783

784+
if ( isPropertyOfRegularEmbeddable( propertyHolder, isComponentEmbedded )
785+
&& property.hasDirectAnnotationUsage(Id.class)) {
786+
throw new AnnotationException("Member '" + property.getName()
787+
+ "' of embeddable class " + propertyHolder.getClassName() + " is annotated '@Id'");
788+
}
789+
784790
final LazyGroup lazyGroupAnnotation = property.getDirectAnnotationUsage( LazyGroup.class );
785791
if ( lazyGroupAnnotation != null ) {
786792
propertyBinder.setLazyGroup( lazyGroupAnnotation.value() );
@@ -805,6 +811,12 @@ private static void buildProperty(
805811
addNaturalIds( inSecondPass, property, columns, joinColumns, context );
806812
}
807813

814+
private static boolean isPropertyOfRegularEmbeddable(PropertyHolder propertyHolder, boolean isComponentEmbedded) {
815+
return propertyHolder.isComponent() // it's a field of some sort of composite value
816+
&& !propertyHolder.isInIdClass() // it's not a field of an id class
817+
&& !isComponentEmbedded; // it's not an entity field matching a field of the id class
818+
}
819+
808820
private static AnnotatedColumns bindProperty(
809821
PropertyHolder propertyHolder,
810822
Nullability nullability,

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/entities/Location.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/**
99
* @author Emmanuel Bernard
1010
*/
11-
@SuppressWarnings("serial")
1211
public class Location implements Serializable {
1312
public double longitude;
1413
public double latitude;

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetoone/embedded/OneToOneLazyOrphanRemovalInEmbeddedEntityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void setCar(Car car) {
113113
@Embeddable
114114
public static class Car {
115115

116-
@Id
116+
@Column(insertable = false, updatable = false)
117117
private Integer id;
118118

119119
// represents a unidirectional one-to-one

0 commit comments

Comments
 (0)