diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializer.java index 683d7a6040..3c64831df2 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializer.java @@ -9,6 +9,7 @@ package org.hibernate.sql.results.graph.entity.internal; import java.util.function.Consumer; import org.hibernate.FetchNotFoundException; +import org.hibernate.Hibernate; import org.hibernate.annotations.NotFoundAction; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; @@ -133,7 +134,7 @@ public class EntitySelectFetchInitializer extends AbstractFetchParentAccess impl final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); entityInstance = persistenceContext.getEntity( entityKey ); - if ( entityInstance != null ) { + if ( entityInstance != null && Hibernate.isInitialized( entityInstance )) { isInitialized = true; return; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java index 3dfb8958ed..78ba5dc4a5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java @@ -7,8 +7,10 @@ package org.hibernate.orm.test.bytecode.enhancement.association; import jakarta.persistence.*; + import org.hibernate.Hibernate; import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; + import org.hibernate.testing.TestForIssue; import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.junit.After; @@ -16,6 +18,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -41,8 +44,8 @@ public class OneToManyLazyAndEagerTest2 extends BaseEntityManagerFunctionalTestC doInJPA( this::entityManagerFactory, em -> { final User user = new User( "User 1", "Marco" ); final User targetUser = new User( "User 2", "Andrea" ); - final Coupon coupon = new Coupon("Coupon 1", targetUser); - final Order order = new Order( "Order 1", user, targetUser, coupon); + final Coupon coupon = new Coupon( "Coupon 1", targetUser ); + final Order order = new Order( "Order 1", user, targetUser, coupon ); em.persist( user ); em.persist( targetUser ); em.persist( coupon ); @@ -65,12 +68,20 @@ public class OneToManyLazyAndEagerTest2 extends BaseEntityManagerFunctionalTestC final Order order = em.createQuery( "select o from Order o", Order.class ) .getResultList() .get( 0 ); + final User user = order.getUser(); assertTrue( "Proxy should be initialized", Hibernate.isInitialized( user ) ); assertEquals( "Marco", user.getName() ); + final User targetUser = order.getTargetUser(); assertTrue( "Proxy should be initialized", Hibernate.isInitialized( targetUser ) ); assertEquals( "Andrea", targetUser.getName() ); + + final Coupon coupon = order.getCoupon(); + assertTrue( "Proxy should be initialized", Hibernate.isInitialized( coupon ) ); + assertThat( coupon.getTargetUser() ).isSameAs( targetUser ); + + } ); } @@ -110,6 +121,10 @@ public class OneToManyLazyAndEagerTest2 extends BaseEntityManagerFunctionalTestC public User getTargetUser() { return targetUser; } + + public Coupon getCoupon() { + return coupon; + } } @Entity(name = "User") @@ -161,5 +176,6 @@ public class OneToManyLazyAndEagerTest2 extends BaseEntityManagerFunctionalTestC public User getTargetUser() { return targetUser; } + } } \ No newline at end of file