HHH-16477 LAZY @ManyToOne may again break EAGER @ManyToOne with Bytecode Enhancement

This commit is contained in:
Andrea Boriero 2023-04-17 16:59:06 +02:00
parent f127aa1ca4
commit a345ed54b2
2 changed files with 20 additions and 3 deletions

View File

@ -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;
}

View File

@ -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;
}
}
}