Skip to content

Commit 1568f89

Browse files
barreirosebersole
authored andcommitted
HHH-9529 - [enhancer] check that the field being accessed belongs to the entity being enhanced
1 parent 4793ca3 commit 1568f89

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/PersistentAttributesEnhancer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ protected void enhanceAttributesAccess(
480480
if ( op != Opcode.PUTFIELD && op != Opcode.GETFIELD ) {
481481
continue;
482482
}
483+
484+
// only transform access to fields of the entity being enhanced
485+
if ( !managedCtClass.getName().equals( constPool.getFieldrefClassName( itr.u16bitAt( index + 1 ) ) ) ) {
486+
continue;
487+
}
488+
483489
final String fieldName = constPool.getFieldrefName( itr.u16bitAt( index + 1 ) );
484490
final PersistentAttributeAccessMethods attributeMethods = attributeDescriptorMap.get( fieldName );
485491

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.hibernate.test.bytecode.enhancement.association.OneToManyAssociationTestTask;
1515
import org.hibernate.test.bytecode.enhancement.association.OneToOneAssociationTestTask;
1616
import org.hibernate.test.bytecode.enhancement.basic.BasicEnhancementTestTask;
17+
import org.hibernate.test.bytecode.enhancement.basic.HHH9529TestTask;
1718
import org.hibernate.test.bytecode.enhancement.dirty.DirtyTrackingTestTask;
1819
import org.hibernate.test.bytecode.enhancement.field.FieldAccessBidirectionalTestTasK;
1920
import org.hibernate.test.bytecode.enhancement.field.FieldAccessEnhancementTestTask;
@@ -30,10 +31,10 @@
3031
import org.hibernate.test.bytecode.enhancement.lazy.group.LazyGroupAccessTestTask;
3132
import org.hibernate.test.bytecode.enhancement.lazyCache.InitFromCacheTestTask;
3233
import org.hibernate.test.bytecode.enhancement.merge.CompositeMergeTestTask;
33-
import org.hibernate.test.bytecode.enhancement.pk.EmbeddedPKTestTask;
3434
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClearedSessionTestTask;
3535
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClosedSessionTestTask;
3636
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyEntityLoadingWithClosedSessionTestTask;
37+
import org.hibernate.test.bytecode.enhancement.pk.EmbeddedPKTestTask;
3738
import org.junit.Test;
3839

3940
/**
@@ -46,6 +47,12 @@ public void testBasic() {
4647
EnhancerTestUtils.runEnhancerTestTask( BasicEnhancementTestTask.class );
4748
}
4849

50+
@Test
51+
@TestForIssue( jiraKey = "HHH-9529" )
52+
public void testFieldHHH9529() {
53+
EnhancerTestUtils.runEnhancerTestTask( HHH9529TestTask.class );
54+
}
55+
4956
@Test
5057
public void testDirty() {
5158
EnhancerTestUtils.runEnhancerTestTask( DirtyTrackingTestTask.class );
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.bytecode.enhancement.basic;
8+
9+
import java.io.Serializable;
10+
import javax.persistence.Embeddable;
11+
import javax.persistence.EmbeddedId;
12+
import javax.persistence.Entity;
13+
import javax.persistence.Id;
14+
import javax.persistence.ManyToOne;
15+
import javax.persistence.MapsId;
16+
17+
import org.hibernate.cfg.Configuration;
18+
import org.hibernate.cfg.Environment;
19+
20+
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
21+
22+
/**
23+
* @author Luis Barreiro
24+
*/
25+
public class HHH9529TestTask extends AbstractEnhancerTestTask {
26+
27+
public Class<?>[] getAnnotatedClasses() {
28+
return new Class<?>[] {Parent.class, Child.class, ChildKey.class};
29+
}
30+
31+
public void prepare() {
32+
Configuration cfg = new Configuration();
33+
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
34+
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
35+
super.prepare( cfg );
36+
}
37+
38+
public void execute() {
39+
}
40+
41+
protected void cleanup() {
42+
}
43+
44+
@Entity
45+
public class Parent {
46+
@Id
47+
String id;
48+
}
49+
50+
@Embeddable
51+
public class ChildKey implements Serializable {
52+
String parent;
53+
String type;
54+
}
55+
56+
@Entity
57+
public class Child {
58+
@EmbeddedId
59+
ChildKey id;
60+
61+
@MapsId("parent")
62+
@ManyToOne
63+
Parent parent;
64+
65+
public String getfieldOnChildKeyParent() {
66+
// Note that there are two GETFIELD ops here, one on the field 'id' that should be enhanced and another
67+
// on the field 'parent' that may be or not (depending if 'extended enhancement' is enabled)
68+
69+
// Either way, the field 'parent' on ChildKey should not be confused with the field 'parent' on Child
70+
71+
return id.parent;
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)