diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java index 51ce1d4090..8eebc4780a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java @@ -6,11 +6,12 @@ */ 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.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; @@ -18,6 +19,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; @@ -31,6 +33,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.mapped.MappedSuperclassTestTask; @@ -89,6 +92,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() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyProxyOnEnhancedEntityTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyProxyOnEnhancedEntityTestTask.java new file mode 100644 index 0000000000..82b4072156 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyProxyOnEnhancedEntityTestTask.java @@ -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 . + */ +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; + } + } + +}