From 8b855d631bc0ea4710fe07d1b2f289fea7c53b0e Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 1 Oct 2019 14:01:01 +0100 Subject: [PATCH] HHH-13640 - Add PrepareStatement executed check to LazyToOnesProxyWithoutSubclassesTest (cherry picked from commit 2bc7fed96ecab48ea6ab98394e0ad93ad829981d) --- .../LazyToOnesProxyWithSubclassesTest.java | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java index da3d77f093..e14705ae9c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java @@ -22,6 +22,7 @@ import org.hibernate.boot.SessionFactoryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; +import org.hibernate.stat.Statistics; import org.hibernate.testing.FailureExpected; import org.hibernate.testing.TestForIssue; @@ -31,6 +32,7 @@ import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -77,16 +79,52 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction inSession( session -> { + final Statistics stats = sessionFactory().getStatistics(); + stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); assertFalse( Hibernate.isInitialized( otherEntity.animal ) ); assertTrue( HibernateProxy.class.isInstance( otherEntity.animal ) ); + + assertEquals( 1, stats.getPrepareStatementCount() ); + Animal animal = session.load( Animal.class, "A Human" ); assertFalse( Hibernate.isInitialized( animal ) ); + assertEquals( 1, stats.getPrepareStatementCount() ); } ); } + @Test + public void testGetInitializeAssociations() { + inTransaction( + session -> { + Human human = new Human( "A Human" ); + OtherEntity otherEntity = new OtherEntity( "test1" ); + otherEntity.animal = human; + + session.persist( human ); + session.persist( otherEntity ); + } + ); + + inSession( + session -> { + final Statistics stats = sessionFactory().getStatistics(); + stats.clear(); + final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); + assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); + assertFalse( Hibernate.isInitialized( otherEntity.animal ) ); + assertEquals( 1, stats.getPrepareStatementCount() ); + + Animal animal = session.get( Animal.class, "A Human" ); + assertTrue( Hibernate.isInitialized( animal ) ); + assertEquals( 2, stats.getPrepareStatementCount() ); + } + ); + + } + @Test public void testExistingProxyAssociation() { inTransaction( @@ -102,6 +140,8 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction inSession( session -> { + final Statistics stats = sessionFactory().getStatistics(); + stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); assertFalse( Hibernate.isInitialized( otherEntity.animal ) ); @@ -109,7 +149,7 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction assertTrue( Hibernate.isPropertyInitialized( otherEntity, "primate" ) ); assertFalse( Hibernate.isInitialized( otherEntity.primate ) ); assertTrue( HibernateProxy.class.isInstance( otherEntity.primate ) ); - + assertEquals( 1, stats.getPrepareStatementCount() ); } ); } @@ -131,6 +171,9 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction inSession( session -> { + final Statistics stats = sessionFactory().getStatistics(); + stats.clear(); + final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); assertFalse( Hibernate.isInitialized( otherEntity.animal ) ); @@ -142,6 +185,8 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction assertFalse( Hibernate.isInitialized( otherEntity.human ) ); // TODO: Should otherEntity.human be a narrowed HibernateProxy or // an uninitialized non-HibernateProxy proxy? + assertEquals( 1, stats.getPrepareStatementCount() ); + } ); } @@ -158,7 +203,6 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction ); } - @Entity(name = "Animal") @Table(name = "Animal") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)