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
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<String, PersistentAttributeAccessMethods> attributeDescriptorMap) {

View File

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