HHH-10540 - [enhancer] Add CompositeOwner interface only once

(cherry picked from commit 37b9a0c903)
This commit is contained in:
barreiro 2016-03-22 01:42:02 +00:00 committed by Gail Badner
parent 81ffaacf09
commit 7ef6400d05
2 changed files with 29 additions and 12 deletions

View File

@ -422,18 +422,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
} }
// make sure to add the CompositeOwner interface // make sure to add the CompositeOwner interface
managedCtClass.addInterface( classPool.get( CompositeOwner.class.getName() ) ); addCompositeOwnerInterface( managedCtClass );
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
);
}
// cleanup previous owner // cleanup previous owner
fieldWriter.insertBefore( 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( protected void enhanceAttributesAccess(
CtClass managedCtClass, CtClass managedCtClass,
IdentityHashMap<String, PersistentAttributeAccessMethods> attributeDescriptorMap) { IdentityHashMap<String, PersistentAttributeAccessMethods> attributeDescriptorMap) {

View File

@ -8,6 +8,7 @@ package org.hibernate.test.bytecode.enhancement.dirty;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.persistence.Embeddable;
import javax.persistence.Embedded; import javax.persistence.Embedded;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
@ -36,6 +37,9 @@ public class SimpleEntity {
@Embedded @Embedded
private Address address; private Address address;
@Embedded
private Address address2;
public Long getId() { public Long getId() {
return id; return id;
} }