HHH-9529 - [enhancer] check that the field being accessed belongs to the entity being enhanced

(cherry picked from commit 1568f89426)
This commit is contained in:
barreiro 2015-10-28 03:39:34 +00:00 committed by Steve Ebersole
parent 682c0642e2
commit 189b379c83
3 changed files with 88 additions and 1 deletions

View File

@ -480,6 +480,12 @@ public class PersistentAttributesEnhancer extends Enhancer {
if ( op != Opcode.PUTFIELD && op != Opcode.GETFIELD ) {
continue;
}
// only transform access to fields of the entity being enhanced
if ( !managedCtClass.getName().equals( constPool.getFieldrefClassName( itr.u16bitAt( index + 1 ) ) ) ) {
continue;
}
final String fieldName = constPool.getFieldrefName( itr.u16bitAt( index + 1 ) );
final PersistentAttributeAccessMethods attributeMethods = attributeDescriptorMap.get( fieldName );

View File

@ -13,6 +13,7 @@ import org.hibernate.test.bytecode.enhancement.association.ManyToManyAssociation
import org.hibernate.test.bytecode.enhancement.association.OneToManyAssociationTestTask;
import org.hibernate.test.bytecode.enhancement.association.OneToOneAssociationTestTask;
import org.hibernate.test.bytecode.enhancement.basic.BasicEnhancementTestTask;
import org.hibernate.test.bytecode.enhancement.basic.HHH9529TestTask;
import org.hibernate.test.bytecode.enhancement.dirty.DirtyTrackingTestTask;
import org.hibernate.test.bytecode.enhancement.field.FieldAccessBidirectionalTestTasK;
import org.hibernate.test.bytecode.enhancement.field.FieldAccessEnhancementTestTask;
@ -27,10 +28,10 @@ import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicFieldAccessTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicPropertyAccessTestTask;
import org.hibernate.test.bytecode.enhancement.merge.CompositeMergeTestTask;
import org.hibernate.test.bytecode.enhancement.pk.EmbeddedPKTestTask;
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClearedSessionTestTask;
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClosedSessionTestTask;
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyEntityLoadingWithClosedSessionTestTask;
import org.hibernate.test.bytecode.enhancement.pk.EmbeddedPKTestTask;
import org.junit.Test;
/**
@ -43,6 +44,12 @@ public class EnhancerTest extends BaseUnitTestCase {
EnhancerTestUtils.runEnhancerTestTask( BasicEnhancementTestTask.class );
}
@Test
@TestForIssue( jiraKey = "HHH-9529" )
public void testFieldHHH9529() {
EnhancerTestUtils.runEnhancerTestTask( HHH9529TestTask.class );
}
@Test
public void testDirty() {
EnhancerTestUtils.runEnhancerTestTask( DirtyTrackingTestTask.class );

View File

@ -0,0 +1,74 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.bytecode.enhancement.basic;
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
/**
* @author Luis Barreiro
*/
public class HHH9529TestTask extends AbstractEnhancerTestTask {
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {Parent.class, Child.class, ChildKey.class};
}
public void prepare() {
Configuration cfg = new Configuration();
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
super.prepare( cfg );
}
public void execute() {
}
protected void cleanup() {
}
@Entity
public class Parent {
@Id
String id;
}
@Embeddable
public class ChildKey implements Serializable {
String parent;
String type;
}
@Entity
public class Child {
@EmbeddedId
ChildKey id;
@MapsId("parent")
@ManyToOne
Parent parent;
public String getfieldOnChildKeyParent() {
// Note that there are two GETFIELD ops here, one on the field 'id' that should be enhanced and another
// on the field 'parent' that may be or not (depending if 'extended enhancement' is enabled)
// Either way, the field 'parent' on ChildKey should not be confused with the field 'parent' on Child
return id.parent;
}
}
}