HHH-10922 - Test case

This commit is contained in:
barreiro 2016-07-15 19:03:29 +01:00 committed by Gail Badner
parent 68a2896c2b
commit 45adb05f5a
2 changed files with 150 additions and 2 deletions

View File

@ -6,12 +6,14 @@
*/
package org.hibernate.test.bytecode.enhancement;
import org.hibernate.test.bytecode.enhancement.eviction.EvictionTestTask;
import org.hibernate.test.bytecode.enhancement.access.MixedAccessTestTask;
import javassist.CtClass;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.test.bytecode.enhancement.access.MixedAccessTestTask;
import org.hibernate.test.bytecode.enhancement.association.ManyToManyAssociationTestTask;
import org.hibernate.test.bytecode.enhancement.association.OneToManyAssociationTestTask;
import org.hibernate.test.bytecode.enhancement.association.OneToOneAssociationTestTask;
@ -19,6 +21,7 @@ import org.hibernate.test.bytecode.enhancement.basic.BasicEnhancementTestTask;
import org.hibernate.test.bytecode.enhancement.basic.HHH9529TestTask;
import org.hibernate.test.bytecode.enhancement.cascade.CascadeDeleteTestTask;
import org.hibernate.test.bytecode.enhancement.dirty.DirtyTrackingTestTask;
import org.hibernate.test.bytecode.enhancement.eviction.EvictionTestTask;
import org.hibernate.test.bytecode.enhancement.extended.ExtendedAssociationManagementTestTasK;
import org.hibernate.test.bytecode.enhancement.extended.ExtendedEnhancementTestTask;
import org.hibernate.test.bytecode.enhancement.join.HHH3949TestTask1;
@ -32,6 +35,7 @@ import org.hibernate.test.bytecode.enhancement.lazy.LazyBasicFieldNotInitialized
import org.hibernate.test.bytecode.enhancement.lazy.LazyCollectionLoadingTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingIntegrationTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyProxyOnEnhancedEntityTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicFieldAccessTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicPropertyAccessTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.group.LazyGroupAccessTestTask;
@ -92,6 +96,17 @@ public class EnhancerTest extends BaseUnitTestCase {
EnhancerTestUtils.runEnhancerTestTask( LazyBasicFieldAccessTestTask.class );
}
@Test
@TestForIssue( jiraKey = "HHH-10922" )
public void testLazyProxyOnEnhancedEntity() {
EnhancerTestUtils.runEnhancerTestTask( LazyProxyOnEnhancedEntityTestTask.class, new EnhancerTestContext() {
@Override
public boolean hasLazyLoadableAttributes(CtClass classDescriptor) {
return false;
}
} );
}
@Test
@TestForIssue( jiraKey = "HHH-10252" )
public void testCascadeDelete() {

View File

@ -0,0 +1,133 @@
/*
* 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.lazy;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.LoadEvent;
import org.hibernate.event.spi.LoadEventListener;
import org.hibernate.jpa.event.internal.core.JpaFlushEventListener;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
/**
* @author Luis Barreiro
*/
public class LazyProxyOnEnhancedEntityTestTask extends AbstractEnhancerTestTask {
private Long parentID;
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {Parent.class, Child.class};
}
public void prepare() {
super.prepare( new Configuration() );
EntityManager em = getFactory().createEntityManager();
em.getTransaction().begin();
Child c = new Child();
em.persist( c );
Parent parent = new Parent();
parent.setChild( c );
em.persist( parent );
parentID = parent.getId();
em.getTransaction().commit();
em.clear();
em.close();
}
public void execute() {
EventListenerRegistry registry = getFactory().unwrap( SessionFactoryImplementor.class ).getServiceRegistry().getService( EventListenerRegistry.class );
registry.prependListeners( EventType.FLUSH, new JpaFlushEventListener() );
registry.prependListeners( EventType.LOAD, new ImmediateLoadTrap() );
EntityManager em = getFactory().createEntityManager();
em.getTransaction().begin();
Parent p = em.find(Parent.class, parentID);
em.flush(); // unwanted lazy load occurs here
em.getTransaction().commit();
em.close();
}
protected void cleanup() {
}
private static class ImmediateLoadTrap implements LoadEventListener {
@Override
public void onLoad(LoadEvent event, LoadEventListener.LoadType loadType) throws HibernateException {
if ( loadType == IMMEDIATE_LOAD ) {
String msg = loadType + ":" + event.getEntityClassName() + "#" + event.getEntityId();
throw new RuntimeException(msg);
}
}
}
@Entity
public static class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(fetch = FetchType.LAZY
)
private Child child;
public Long getId() {
return id;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
}
@Entity
public static class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}