diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/PersistentAttributesEnhancer.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/PersistentAttributesEnhancer.java index c4cbfd610b..bc7e7781a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/PersistentAttributesEnhancer.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/PersistentAttributesEnhancer.java @@ -422,18 +422,7 @@ public class PersistentAttributesEnhancer extends Enhancer { } // make sure to add the CompositeOwner interface - managedCtClass.addInterface( classPool.get( CompositeOwner.class.getName() ) ); - - if ( enhancementContext.isCompositeClass( managedCtClass ) ) { - // if a composite have a embedded field we need to implement the TRACKER_CHANGER_NAME method as well - MethodWriter.write( - managedCtClass, - "public void %1$s(String name) {%n" + - " if (%2$s != null) { %2$s.callOwner(\".\" + name); }%n}", - EnhancerConstants.TRACKER_CHANGER_NAME, - EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME - ); - } + addCompositeOwnerInterface( managedCtClass ); // cleanup previous owner fieldWriter.insertBefore( @@ -459,6 +448,30 @@ public class PersistentAttributesEnhancer extends Enhancer { ); } + private void addCompositeOwnerInterface(CtClass managedCtClass) throws NotFoundException, CannotCompileException { + CtClass compositeOwnerCtClass = managedCtClass.getClassPool().get( CompositeOwner.class.getName() ); + + // HHH-10540 only add the interface once + for ( CtClass i : managedCtClass.getInterfaces() ) { + if ( i.subclassOf( compositeOwnerCtClass ) ) { + return; + } + } + + managedCtClass.addInterface( compositeOwnerCtClass ); + + if ( enhancementContext.isCompositeClass( managedCtClass ) ) { + // if a composite have a embedded field we need to implement the TRACKER_CHANGER_NAME method as well + MethodWriter.write( + managedCtClass, + "public void %1$s(String name) {%n" + + " if (%2$s != null) { %2$s.callOwner(\".\" + name); }%n}", + EnhancerConstants.TRACKER_CHANGER_NAME, + EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME + ); + } + } + protected void enhanceAttributesAccess( CtClass managedCtClass, IdentityHashMap attributeDescriptorMap) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/dirty/SimpleEntity.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/dirty/SimpleEntity.java index c11763040d..5d438ef1e4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/dirty/SimpleEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/dirty/SimpleEntity.java @@ -8,6 +8,7 @@ package org.hibernate.test.bytecode.enhancement.dirty; import java.util.List; import java.util.Set; +import javax.persistence.Embeddable; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.Id; @@ -36,6 +37,9 @@ public class SimpleEntity { @Embedded private Address address; + @Embedded + private Address address2; + public Long getId() { return id; }