HHH-10646 - [enhancer] Add support for @MappedSuperclass

(cherry picked from commit e615d76a26)

Conflicts:
	hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java
This commit is contained in:
barreiro 2016-05-18 17:36:08 +01:00 committed by Gail Badner
parent 4f3391a592
commit 2aa76b3ef9
5 changed files with 121 additions and 14 deletions

View File

@ -10,6 +10,9 @@ import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.persistence.MappedSuperclass;
import javassist.CannotCompileException;
import javassist.CtClass;
@ -208,24 +211,33 @@ public class EntityEnhancer extends Enhancer {
private List<CtField> collectCollectionFields(CtClass managedCtClass) {
final List<CtField> collectionList = new LinkedList<CtField>();
try {
for ( CtField ctField : managedCtClass.getDeclaredFields() ) {
// skip static fields and skip fields added by enhancement
if ( Modifier.isStatic( ctField.getModifiers() ) || ctField.getName().startsWith( "$$_hibernate_" ) ) {
continue;
}
if ( enhancementContext.isPersistentField( ctField ) ) {
for ( CtClass ctClass : ctField.getType().getInterfaces() ) {
if ( PersistentAttributesHelper.isAssignable( ctClass, Collection.class.getName() ) ) {
collectionList.add( ctField );
break;
}
}
for ( CtField ctField : managedCtClass.getDeclaredFields() ) {
// skip static fields and skip fields added by enhancement
if ( Modifier.isStatic( ctField.getModifiers() ) || ctField.getName().startsWith( "$$_hibernate_" ) ) {
continue;
}
if ( enhancementContext.isPersistentField( ctField ) ) {
if ( PersistentAttributesHelper.isAssignable( ctField, Collection.class.getName() ) ||
PersistentAttributesHelper.isAssignable( ctField, Map.class.getName() ) ) {
collectionList.add( ctField );
}
}
}
catch (NotFoundException ignored) {
// HHH-10646 Add fields inherited from @MappedSuperclass
for ( CtField ctField : managedCtClass.getDeclaredFields() ) {
if ( !ctField.getDeclaringClass().hasAnnotation( MappedSuperclass.class ) || Modifier.isStatic( ctField.getModifiers() ) ) {
continue;
}
if ( enhancementContext.isPersistentField( ctField ) ) {
if ( PersistentAttributesHelper.isAssignable( ctField, Collection.class.getName() ) ||
PersistentAttributesHelper.isAssignable( ctField, Map.class.getName() ) ) {
collectionList.add( ctField );
}
}
}
return collectionList;
}

View File

@ -14,6 +14,7 @@ import javax.persistence.Embedded;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@ -90,6 +91,16 @@ public class PersistentAttributesEnhancer extends Enhancer {
persistentFieldList.add( ctField );
}
}
// HHH-10646 Add fields inherited from @MappedSuperclass
// CtClass.getFields() does not return private fields, while CtClass.getDeclaredFields() does not return inherit
for ( CtField ctField : managedCtClass.getFields() ) {
if ( !ctField.getDeclaringClass().hasAnnotation( MappedSuperclass.class ) || Modifier.isStatic( ctField.getModifiers() ) ) {
continue;
}
if ( enhancementContext.isPersistentField( ctField ) ) {
persistentFieldList.add( ctField );
}
}
return enhancementContext.order( persistentFieldList.toArray( new CtField[persistentFieldList.size()] ) );
}

View File

@ -383,4 +383,14 @@ public class PersistentAttributesHelper {
return false;
}
public static boolean isAssignable(CtField thisCtField, String targetClassName) {
try {
return isAssignable( thisCtField.getType(), targetClassName );
}
catch (NotFoundException e) {
// keep going
}
return false;
}
}

View File

@ -33,6 +33,7 @@ import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingIntegrationTestTa
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.mapped.MappedSuperclassTestTask;
import org.hibernate.test.bytecode.enhancement.merge.CompositeMergeTestTask;
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClearedSessionTestTask;
import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClosedSessionTestTask;
@ -117,6 +118,12 @@ public class EnhancerTest extends BaseUnitTestCase {
EnhancerTestUtils.runEnhancerTestTask( UnexpectedDeleteThreeTestTask.class );
}
@Test
@TestForIssue( jiraKey = "HHH-10646" )
public void testMappedSuperclass() {
EnhancerTestUtils.runEnhancerTestTask( MappedSuperclassTestTask.class );
}
@Test
public void testMerge() {
EnhancerTestUtils.runEnhancerTestTask( CompositeMergeTestTask.class );

View File

@ -0,0 +1,67 @@
package org.hibernate.test.bytecode.enhancement.mapped;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
/**
* @author Luis Barreiro
*/
public class MappedSuperclassTestTask extends AbstractEnhancerTestTask {
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Person.class, Employee.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() {
Employee charles = new Employee( "Charles", "Engineer" );
charles.oca = 1002;
// Check that both types of class attributes are being dirty tracked
EnhancerTestUtils.checkDirtyTracking( charles, "title", "oca" );
}
protected void cleanup() {
}
@MappedSuperclass private static class Person {
@Id String name;
@Version long oca;
public Person(String name) {
this();
this.name = name;
}
protected Person() {}
}
@Entity private static class Employee extends Person {
private String title;
public Employee(String name, String title) {
super(name);
this.title = title;
}
public Employee() {}
}
}