Skip to content

Commit 2aa76b3

Browse files
barreirogbadner
authored andcommitted
HHH-10646 - [enhancer] Add support for @MappedSuperclass
(cherry picked from commit e615d76) Conflicts: hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java
1 parent 4f3391a commit 2aa76b3

File tree

5 files changed

+121
-14
lines changed

5 files changed

+121
-14
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import java.util.LinkedList;
1111
import java.util.List;
1212
import java.util.Locale;
13+
import java.util.Map;
14+
15+
import javax.persistence.MappedSuperclass;
1316

1417
import javassist.CannotCompileException;
1518
import javassist.CtClass;
@@ -208,24 +211,33 @@ private void createDirtyTrackerMethods(CtClass managedCtClass) {
208211

209212
private List<CtField> collectCollectionFields(CtClass managedCtClass) {
210213
final List<CtField> collectionList = new LinkedList<CtField>();
211-
try {
212-
for ( CtField ctField : managedCtClass.getDeclaredFields() ) {
213-
// skip static fields and skip fields added by enhancement
214-
if ( Modifier.isStatic( ctField.getModifiers() ) || ctField.getName().startsWith( "$$_hibernate_" ) ) {
215-
continue;
216-
}
217-
if ( enhancementContext.isPersistentField( ctField ) ) {
218-
for ( CtClass ctClass : ctField.getType().getInterfaces() ) {
219-
if ( PersistentAttributesHelper.isAssignable( ctClass, Collection.class.getName() ) ) {
220-
collectionList.add( ctField );
221-
break;
222-
}
223-
}
214+
215+
for ( CtField ctField : managedCtClass.getDeclaredFields() ) {
216+
// skip static fields and skip fields added by enhancement
217+
if ( Modifier.isStatic( ctField.getModifiers() ) || ctField.getName().startsWith( "$$_hibernate_" ) ) {
218+
continue;
219+
}
220+
if ( enhancementContext.isPersistentField( ctField ) ) {
221+
if ( PersistentAttributesHelper.isAssignable( ctField, Collection.class.getName() ) ||
222+
PersistentAttributesHelper.isAssignable( ctField, Map.class.getName() ) ) {
223+
collectionList.add( ctField );
224224
}
225225
}
226226
}
227-
catch (NotFoundException ignored) {
227+
228+
// HHH-10646 Add fields inherited from @MappedSuperclass
229+
for ( CtField ctField : managedCtClass.getDeclaredFields() ) {
230+
if ( !ctField.getDeclaringClass().hasAnnotation( MappedSuperclass.class ) || Modifier.isStatic( ctField.getModifiers() ) ) {
231+
continue;
232+
}
233+
if ( enhancementContext.isPersistentField( ctField ) ) {
234+
if ( PersistentAttributesHelper.isAssignable( ctField, Collection.class.getName() ) ||
235+
PersistentAttributesHelper.isAssignable( ctField, Map.class.getName() ) ) {
236+
collectionList.add( ctField );
237+
}
238+
}
228239
}
240+
229241
return collectionList;
230242
}
231243

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.persistence.Id;
1515
import javax.persistence.ManyToMany;
1616
import javax.persistence.ManyToOne;
17+
import javax.persistence.MappedSuperclass;
1718
import javax.persistence.OneToMany;
1819
import javax.persistence.OneToOne;
1920

@@ -90,6 +91,16 @@ private CtField[] collectPersistentFields(CtClass managedCtClass) {
9091
persistentFieldList.add( ctField );
9192
}
9293
}
94+
// HHH-10646 Add fields inherited from @MappedSuperclass
95+
// CtClass.getFields() does not return private fields, while CtClass.getDeclaredFields() does not return inherit
96+
for ( CtField ctField : managedCtClass.getFields() ) {
97+
if ( !ctField.getDeclaringClass().hasAnnotation( MappedSuperclass.class ) || Modifier.isStatic( ctField.getModifiers() ) ) {
98+
continue;
99+
}
100+
if ( enhancementContext.isPersistentField( ctField ) ) {
101+
persistentFieldList.add( ctField );
102+
}
103+
}
93104
return enhancementContext.order( persistentFieldList.toArray( new CtField[persistentFieldList.size()] ) );
94105
}
95106

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,14 @@ public static boolean isAssignable(CtClass thisCtClass, String targetClassName)
383383
return false;
384384
}
385385

386+
public static boolean isAssignable(CtField thisCtField, String targetClassName) {
387+
try {
388+
return isAssignable( thisCtField.getType(), targetClassName );
389+
}
390+
catch (NotFoundException e) {
391+
// keep going
392+
}
393+
return false;
394+
}
395+
386396
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingTestTask;
3434
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicFieldAccessTestTask;
3535
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicPropertyAccessTestTask;
36+
import org.hibernate.test.bytecode.enhancement.mapped.MappedSuperclassTestTask;
3637
import org.hibernate.test.bytecode.enhancement.merge.CompositeMergeTestTask;
3738
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClearedSessionTestTask;
3839
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClosedSessionTestTask;
@@ -117,6 +118,12 @@ public void testLazyUnexpectedDelete() {
117118
EnhancerTestUtils.runEnhancerTestTask( UnexpectedDeleteThreeTestTask.class );
118119
}
119120

121+
@Test
122+
@TestForIssue( jiraKey = "HHH-10646" )
123+
public void testMappedSuperclass() {
124+
EnhancerTestUtils.runEnhancerTestTask( MappedSuperclassTestTask.class );
125+
}
126+
120127
@Test
121128
public void testMerge() {
122129
EnhancerTestUtils.runEnhancerTestTask( CompositeMergeTestTask.class );
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.hibernate.test.bytecode.enhancement.mapped;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.MappedSuperclass;
6+
import javax.persistence.Version;
7+
8+
import org.hibernate.Session;
9+
import org.hibernate.cfg.Configuration;
10+
import org.hibernate.cfg.Environment;
11+
12+
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
13+
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
14+
15+
/**
16+
* @author Luis Barreiro
17+
*/
18+
public class MappedSuperclassTestTask extends AbstractEnhancerTestTask {
19+
20+
public Class<?>[] getAnnotatedClasses() {
21+
return new Class<?>[] { Person.class, Employee.class };
22+
}
23+
24+
public void prepare() {
25+
Configuration cfg = new Configuration();
26+
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
27+
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
28+
super.prepare( cfg );
29+
}
30+
31+
public void execute() {
32+
Employee charles = new Employee( "Charles", "Engineer" );
33+
charles.oca = 1002;
34+
35+
// Check that both types of class attributes are being dirty tracked
36+
EnhancerTestUtils.checkDirtyTracking( charles, "title", "oca" );
37+
}
38+
39+
protected void cleanup() {
40+
}
41+
42+
@MappedSuperclass private static class Person {
43+
44+
@Id String name;
45+
46+
@Version long oca;
47+
48+
public Person(String name) {
49+
this();
50+
this.name = name;
51+
}
52+
53+
protected Person() {}
54+
}
55+
56+
@Entity private static class Employee extends Person {
57+
58+
private String title;
59+
60+
public Employee(String name, String title) {
61+
super(name);
62+
this.title = title;
63+
}
64+
65+
public Employee() {}
66+
}
67+
}

0 commit comments

Comments
 (0)