HHH-12687 : fix test case to work in 5.1

This commit is contained in:
Gail Badner 2018-06-15 15:31:09 -07:00
parent b73f0d90e6
commit ddc24e8659
1 changed files with 75 additions and 56 deletions

View File

@ -18,12 +18,12 @@ import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -47,24 +47,31 @@ public class EmbeddableCollectionElementWithLazyManyToOneTest extends BaseCoreFu
Parent p = new Parent(); Parent p = new Parent();
p.containedChild = new ContainedChild( new Child() ); p.containedChild = new ContainedChild( new Child() );
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.persist( p ); session.persist( p );
}
);
doInHibernate( }
this::sessionFactory, session -> { session.getTransaction().commit();
Parent pRead = session.get( Parent.class, p.id ); session.close();
session = openSession();
session.beginTransaction();
{
Parent pRead = (Parent) session.get( Parent.class, p.id );
assertFalse( Hibernate.isInitialized( pRead.containedChild.child ) ); assertFalse( Hibernate.isInitialized( pRead.containedChild.child ) );
} }
); session.getTransaction().commit();
session.close();
doInHibernate( session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.delete( p ); session.delete( p );
} }
); session.getTransaction().commit();
session.close();
} }
@Test @Test
@ -73,27 +80,33 @@ public class EmbeddableCollectionElementWithLazyManyToOneTest extends BaseCoreFu
Parent p = new Parent(); Parent p = new Parent();
p.containedChildren.add( new ContainedChild( new Child() ) ); p.containedChildren.add( new ContainedChild( new Child() ) );
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.persist( p ); session.persist( p );
} }
); session.getTransaction().commit();
session.close();
doInHibernate( session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
Parent pRead = session.get( Parent.class, p.id ); {
Parent pRead = (Parent) session.get( Parent.class, p.id );
assertFalse( Hibernate.isInitialized( pRead.containedChildren ) ); assertFalse( Hibernate.isInitialized( pRead.containedChildren ) );
assertEquals( 1, pRead.containedChildren.size() ); assertEquals( 1, pRead.containedChildren.size() );
assertTrue( Hibernate.isInitialized( pRead.containedChildren ) ); assertTrue( Hibernate.isInitialized( pRead.containedChildren ) );
assertFalse( Hibernate.isInitialized( pRead.containedChildren.iterator().next().child ) ); assertFalse( Hibernate.isInitialized( pRead.containedChildren.iterator().next().child ) );
} }
); session.getTransaction().commit();
session.close();
doInHibernate( session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.delete( p ); session.delete( p );
} }
); session.getTransaction().commit();
session.close();
} }
@Test @Test
@ -104,28 +117,34 @@ public class EmbeddableCollectionElementWithLazyManyToOneTest extends BaseCoreFu
p.containedChild = containedChild; p.containedChild = containedChild;
p.containedChildren.add( containedChild ); p.containedChildren.add( containedChild );
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.persist( p ); session.persist( p );
} }
); session.getTransaction().commit();
session.close();
doInHibernate( session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
Parent pRead = session.get( Parent.class, p.id ); {
Parent pRead = (Parent) session.get( Parent.class, p.id );
assertFalse( Hibernate.isInitialized( pRead.containedChild.child ) ); assertFalse( Hibernate.isInitialized( pRead.containedChild.child ) );
assertFalse( Hibernate.isInitialized( pRead.containedChildren ) ); assertFalse( Hibernate.isInitialized( pRead.containedChildren ) );
assertEquals( 1, pRead.containedChildren.size() ); assertEquals( 1, pRead.containedChildren.size() );
assertTrue( Hibernate.isInitialized( pRead.containedChildren ) ); assertTrue( Hibernate.isInitialized( pRead.containedChildren ) );
assertFalse( Hibernate.isInitialized( pRead.containedChildren.iterator().next().child ) ); assertFalse( Hibernate.isInitialized( pRead.containedChildren.iterator().next().child ) );
} }
); session.getTransaction().commit();
session.close();
doInHibernate( session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.delete( p ); session.delete( p );
} }
); session.getTransaction().commit();
session.close();
} }
@Entity(name = "Parent") @Entity(name = "Parent")