diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java index 509ee7badf..3b5c7b4a1e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java @@ -44,6 +44,7 @@ import org.hibernate.jpa.test.pack.various.Seat; import org.hibernate.stat.Statistics; import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.transaction.TransactionUtil; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -80,32 +81,29 @@ public class PackagedEntityManagerTest extends PackagingTestCase { // run the test emf = Persistence.createEntityManagerFactory( "defaultpar", new HashMap() ); - EntityManager em = emf.createEntityManager(); - ApplicationServer as = new ApplicationServer(); - as.setName( "JBoss AS" ); - Version v = new Version(); - v.setMajor( 4 ); - v.setMinor( 0 ); - v.setMicro( 3 ); - as.setVersion( v ); - Mouse mouse = new Mouse(); - mouse.setName( "mickey" ); - em.getTransaction().begin(); - em.persist( as ); - em.persist( mouse ); - assertEquals( 1, em.createNamedQuery( "allMouse" ).getResultList().size() ); - Lighter lighter = new Lighter(); - lighter.name = "main"; - lighter.power = " 250 W"; - em.persist( lighter ); - em.flush(); - em.remove( lighter ); - em.remove( mouse ); - assertNotNull( as.getId() ); - em.remove( as ); - em.getTransaction().commit(); - em.close(); - emf.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + ApplicationServer as = new ApplicationServer(); + as.setName( "JBoss AS" ); + Version v = new Version(); + v.setMajor( 4 ); + v.setMinor( 0 ); + v.setMicro( 3 ); + as.setVersion( v ); + Mouse mouse = new Mouse(); + mouse.setName( "mickey" ); + em.persist( as ); + em.persist( mouse ); + assertEquals( 1, em.createNamedQuery( "allMouse" ).getResultList().size() ); + Lighter lighter = new Lighter(); + lighter.name = "main"; + lighter.power = " 250 W"; + em.persist( lighter ); + em.flush(); + em.remove( lighter ); + em.remove( mouse ); + assertNotNull( as.getId() ); + em.remove( as ); + } ); } @Test @@ -114,32 +112,29 @@ public class PackagedEntityManagerTest extends PackagingTestCase { addPackageToClasspath( testPackage ); emf = Persistence.createEntityManagerFactory( "defaultpar_1_0", new HashMap() ); - EntityManager em = emf.createEntityManager(); - ApplicationServer1 as = new ApplicationServer1(); - as.setName( "JBoss AS" ); - Version1 v = new Version1(); - v.setMajor( 4 ); - v.setMinor( 0 ); - v.setMicro( 3 ); - as.setVersion( v ); - Mouse1 mouse = new Mouse1(); - mouse.setName( "mickey" ); - em.getTransaction().begin(); - em.persist( as ); - em.persist( mouse ); - assertEquals( 1, em.createNamedQuery( "allMouse_1_0" ).getResultList().size() ); - Lighter1 lighter = new Lighter1(); - lighter.name = "main"; - lighter.power = " 250 W"; - em.persist( lighter ); - em.flush(); - em.remove( lighter ); - em.remove( mouse ); - assertNotNull( as.getId() ); - em.remove( as ); - em.getTransaction().commit(); - em.close(); - emf.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + ApplicationServer1 as = new ApplicationServer1(); + as.setName( "JBoss AS" ); + Version1 v = new Version1(); + v.setMajor( 4 ); + v.setMinor( 0 ); + v.setMicro( 3 ); + as.setVersion( v ); + Mouse1 mouse = new Mouse1(); + mouse.setName( "mickey" ); + em.persist( as ); + em.persist( mouse ); + assertEquals( 1, em.createNamedQuery( "allMouse_1_0" ).getResultList().size() ); + Lighter1 lighter = new Lighter1(); + lighter.name = "main"; + lighter.power = " 250 W"; + em.persist( lighter ); + em.flush(); + em.remove( lighter ); + em.remove( mouse ); + assertNotNull( as.getId() ); + em.remove( as ); + } ); } @Test @@ -150,36 +145,33 @@ public class PackagedEntityManagerTest extends PackagingTestCase { IncrementListener.reset(); OtherIncrementListener.reset(); emf = Persistence.createEntityManagerFactory( "defaultpar", new HashMap() ); - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - ApplicationServer as = new ApplicationServer(); - as.setName( "JBoss AS" ); - Version v = new Version(); - v.setMajor( 4 ); - v.setMinor( 0 ); - v.setMicro( 3 ); - as.setVersion( v ); - em.persist( as ); - em.flush(); - assertEquals( 1, IncrementListener.getIncrement(), "Failure in default listeners" ); - assertEquals( 1, OtherIncrementListener.getIncrement(), "Failure in XML overriden listeners" ); + TransactionUtil.doInJPA( () -> emf, em -> { + ApplicationServer as = new ApplicationServer(); + as.setName( "JBoss AS" ); + Version v = new Version(); + v.setMajor( 4 ); + v.setMinor( 0 ); + v.setMicro( 3 ); + as.setVersion( v ); + em.persist( as ); + em.flush(); + assertEquals( 1, IncrementListener.getIncrement(), "Failure in default listeners" ); + assertEquals( 1, OtherIncrementListener.getIncrement(), "Failure in XML overriden listeners" ); - Mouse mouse = new Mouse(); - mouse.setName( "mickey" ); - em.persist( mouse ); - em.flush(); - assertEquals( 1, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" ); - assertEquals( 1, OtherIncrementListener.getIncrement() ); + Mouse mouse = new Mouse(); + mouse.setName( "mickey" ); + em.persist( mouse ); + em.flush(); + assertEquals( 1, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" ); + assertEquals( 1, OtherIncrementListener.getIncrement() ); - Money money = new Money(); - em.persist( money ); - em.flush(); - assertEquals( 2, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" ); - assertEquals( 1, OtherIncrementListener.getIncrement() ); - - em.getTransaction().rollback(); - em.close(); - emf.close(); + Money money = new Money(); + em.persist( money ); + em.flush(); + assertEquals( 2, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" ); + assertEquals( 1, OtherIncrementListener.getIncrement() ); + em.getTransaction().setRollbackOnly(); + } ); } @Test @@ -188,20 +180,17 @@ public class PackagedEntityManagerTest extends PackagingTestCase { addPackageToClasspath( testPackage ); emf = Persistence.createEntityManagerFactory( "explodedpar", new HashMap() ); - EntityManager em = emf.createEntityManager(); - Carpet carpet = new Carpet(); - Elephant el = new Elephant(); - el.setName( "Dumbo" ); - carpet.setCountry( "Turkey" ); - em.getTransaction().begin(); - em.persist( carpet ); - em.persist( el ); - assertEquals( 1, em.createNamedQuery( "allCarpet" ).getResultList().size() ); - assertNotNull( carpet.getId() ); - em.remove( carpet ); - em.getTransaction().commit(); - em.close(); - emf.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + Carpet carpet = new Carpet(); + Elephant el = new Elephant(); + el.setName( "Dumbo" ); + carpet.setCountry( "Turkey" ); + em.persist( carpet ); + em.persist( el ); + assertEquals( 1, em.createNamedQuery( "allCarpet" ).getResultList().size() ); + assertNotNull( carpet.getId() ); + em.remove( carpet ); + } ); } @Test @@ -228,18 +217,15 @@ public class PackagedEntityManagerTest extends PackagingTestCase { fail( "Try to process hbm file: " + e.getMessage() ); } - EntityManager em = emf.createEntityManager(); - Caipirinha s = new Caipirinha( "Strong" ); - em.getTransaction().begin(); - em.persist( s ); - em.getTransaction().commit(); + TransactionUtil.doInJPA( () -> emf, em -> { + Caipirinha s = new Caipirinha( "Strong" ); + em.persist( s ); + em.getTransaction().commit(); - em.getTransaction().begin(); - s = em.find( Caipirinha.class, s.getId() ); - em.remove( s ); - em.getTransaction().commit(); - em.close(); - emf.close(); + em.getTransaction().begin(); + s = em.find( Caipirinha.class, s.getId() ); + em.remove( s ); + } ); } @Test @@ -251,24 +237,21 @@ public class PackagedEntityManagerTest extends PackagingTestCase { assertTrue( emf.getProperties().containsKey( "hibernate.test-assertable-setting" ) ); - EntityManager em = emf.createEntityManager(); - Item i = new Item(); - i.setDescr( "Blah" ); - i.setName( "factory" ); - Morito m = new Morito(); - m.setPower( "SuperStrong" ); - em.getTransaction().begin(); - em.persist( i ); - em.persist( m ); - em.getTransaction().commit(); + TransactionUtil.doInJPA( () -> emf, em -> { + Item i = new Item(); + i.setDescr( "Blah" ); + i.setName( "factory" ); + Morito m = new Morito(); + m.setPower( "SuperStrong" ); + em.persist( i ); + em.persist( m ); + em.getTransaction().commit(); - em.getTransaction().begin(); - i = em.find( Item.class, i.getName() ); - em.remove( i ); - em.remove( em.find( Morito.class, m.getId() ) ); - em.getTransaction().commit(); - em.close(); - emf.close(); + em.getTransaction().begin(); + i = em.find( Item.class, i.getName() ); + em.remove( i ); + em.remove( em.find( Morito.class, m.getId() ) ); + } ); } @Test @@ -277,17 +260,15 @@ public class PackagedEntityManagerTest extends PackagingTestCase { addPackageToClasspath( testPackage ); emf = Persistence.createEntityManagerFactory( "space par", new HashMap() ); - EntityManager em = emf.createEntityManager(); - org.hibernate.jpa.test.pack.spacepar.Bug bug = new org.hibernate.jpa.test.pack.spacepar.Bug(); - bug.setSubject( "Spaces in directory name don't play well on Windows" ); - em.getTransaction().begin(); - em.persist( bug ); - em.flush(); - em.remove( bug ); - assertNotNull( bug.getId() ); - em.getTransaction().rollback(); - em.close(); - emf.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + org.hibernate.jpa.test.pack.spacepar.Bug bug = new org.hibernate.jpa.test.pack.spacepar.Bug(); + bug.setSubject( "Spaces in directory name don't play well on Windows" ); + em.persist( bug ); + em.flush(); + em.remove( bug ); + assertNotNull( bug.getId() ); + em.getTransaction().setRollbackOnly(); + } ); } @Test @@ -301,17 +282,16 @@ public class PackagedEntityManagerTest extends PackagingTestCase { p.load( ConfigHelper.getResourceAsStream( "/overridenpar.properties" ) ); properties.putAll( p ); emf = Persistence.createEntityManagerFactory( "overridenpar", properties ); - EntityManager em = emf.createEntityManager(); - org.hibernate.jpa.test.pack.overridenpar.Bug bug = new org.hibernate.jpa.test.pack.overridenpar.Bug(); - bug.setSubject( "Allow DS overriding" ); - em.getTransaction().begin(); - em.persist( bug ); - em.flush(); - em.remove( bug ); - assertNotNull( bug.getId() ); - em.getTransaction().rollback(); - em.close(); - emf.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + org.hibernate.jpa.test.pack.overridenpar.Bug bug = new org.hibernate.jpa.test.pack.overridenpar.Bug(); + bug.setSubject( "Allow DS overriding" ); + em.getTransaction().begin(); + em.persist( bug ); + em.flush(); + em.remove( bug ); + assertNotNull( bug.getId() ); + em.getTransaction().setRollbackOnly(); + } ); } @Test @@ -321,17 +301,19 @@ public class PackagedEntityManagerTest extends PackagingTestCase { emf = Persistence.createEntityManagerFactory( "manager1", new HashMap() ); EntityManager em = emf.createEntityManager(); - EventListenerRegistry listenerRegistry = em.unwrap( SharedSessionContractImplementor.class ).getFactory() - .getServiceRegistry() - .getService( EventListenerRegistry.class ); - assertEquals( - listenerRegistry.getEventListenerGroup( EventType.PRE_INSERT ).count(), - listenerRegistry.getEventListenerGroup( EventType.PRE_UPDATE ).count() + 1, - "Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work" - ); - - em.close(); - emf.close(); + try { + EventListenerRegistry listenerRegistry = em.unwrap( SharedSessionContractImplementor.class ).getFactory() + .getServiceRegistry() + .getService( EventListenerRegistry.class ); + assertEquals( + listenerRegistry.getEventListenerGroup( EventType.PRE_INSERT ).count(), + listenerRegistry.getEventListenerGroup( EventType.PRE_UPDATE ).count() + 1, + "Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work" + ); + } + finally { + em.close(); + } } @Test @@ -340,48 +322,44 @@ public class PackagedEntityManagerTest extends PackagingTestCase { addPackageToClasspath( testPackage ); emf = Persistence.createEntityManagerFactory( "manager1", new HashMap() ); - EntityManager em = emf.createEntityManager(); - Item item = new Item( "Mouse", "Micro$oft mouse" ); - em.getTransaction().begin(); - em.persist( item ); - assertTrue( em.contains( item ) ); - em.getTransaction().commit(); + TransactionUtil.doInJPA( () -> emf, em -> { + Item item = new Item( "Mouse", "Micro$oft mouse" ); + em.persist( item ); + assertTrue( em.contains( item ) ); + em.getTransaction().commit(); - assertTrue( em.contains( item ) ); + assertTrue( em.contains( item ) ); - em.getTransaction().begin(); - Item item1 = (Item) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult(); - assertNotNull( item1 ); - assertSame( item, item1 ); - item.setDescr( "Micro$oft wireless mouse" ); - assertTrue( em.contains( item ) ); - em.getTransaction().commit(); + em.getTransaction().begin(); + Item item1 = (Item) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult(); + assertNotNull( item1 ); + assertSame( item, item1 ); + item.setDescr( "Micro$oft wireless mouse" ); + assertTrue( em.contains( item ) ); + em.getTransaction().commit(); - assertTrue( em.contains( item ) ); + assertTrue( em.contains( item ) ); - em.getTransaction().begin(); - item1 = em.find( Item.class, "Mouse" ); - assertSame( item, item1 ); - em.getTransaction().commit(); - assertTrue( em.contains( item ) ); + em.getTransaction().begin(); + item1 = em.find( Item.class, "Mouse" ); + assertSame( item, item1 ); + em.getTransaction().commit(); + assertTrue( em.contains( item ) ); - item1 = em.find( Item.class, "Mouse" ); - assertSame( item, item1 ); - assertTrue( em.contains( item ) ); + item1 = em.find( Item.class, "Mouse" ); + assertSame( item, item1 ); + assertTrue( em.contains( item ) ); - item1 = (Item) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult(); - assertNotNull( item1 ); - assertSame( item, item1 ); - assertTrue( em.contains( item ) ); + item1 = (Item) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult(); + assertNotNull( item1 ); + assertSame( item, item1 ); + assertTrue( em.contains( item ) ); - em.getTransaction().begin(); - assertTrue( em.contains( item ) ); - em.remove( item ); - em.remove( item ); //second remove should be a no-op - em.getTransaction().commit(); - - em.close(); - emf.close(); + em.getTransaction().begin(); + assertTrue( em.contains( item ) ); + em.remove( item ); + em.remove( item ); //second remove should be a no-op + } ); } @Test @@ -400,42 +378,33 @@ public class PackagedEntityManagerTest extends PackagingTestCase { stats.clear(); stats.setStatisticsEnabled( true ); - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - - em.persist( res ); - em.persist( item ); - assertTrue( em.contains( item ) ); - - em.getTransaction().commit(); - em.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + em.persist( res ); + em.persist( item ); + assertTrue( em.contains( item ) ); + } ); assertEquals( 1, stats.getSecondLevelCachePutCount() ); assertEquals( 0, stats.getSecondLevelCacheHitCount() ); - em = emf.createEntityManager(); - em.getTransaction().begin(); - Item second = em.find( Item.class, item.getName() ); - assertEquals( 1, second.getDistributors().size() ); - assertEquals( 1, stats.getSecondLevelCacheHitCount() ); - em.getTransaction().commit(); - em.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + Item second = em.find( Item.class, item.getName() ); + assertEquals( 1, second.getDistributors().size() ); + assertEquals( 1, stats.getSecondLevelCacheHitCount() ); + } ); - em = emf.createEntityManager(); - em.getTransaction().begin(); - second = em.find( Item.class, item.getName() ); - assertEquals( 1, second.getDistributors().size() ); - assertEquals( 3, stats.getSecondLevelCacheHitCount() ); - for ( Distributor distro : second.getDistributors() ) { - em.remove( distro ); - } - em.remove( second ); - em.getTransaction().commit(); - em.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + Item second = em.find( Item.class, item.getName() ); + assertEquals( 1, second.getDistributors().size() ); + assertEquals( 3, stats.getSecondLevelCacheHitCount() ); + for ( Distributor distro : second.getDistributors() ) { + em.remove( distro ); + } + em.remove( second ); + } ); stats.clear(); stats.setStatisticsEnabled( false ); - emf.close(); } @Test @@ -445,22 +414,18 @@ public class PackagedEntityManagerTest extends PackagingTestCase { addPackageToClasspath( testPackage, externalJar ); emf = Persistence.createEntityManagerFactory( "manager1", new HashMap() ); - EntityManager em = emf.createEntityManager(); - Scooter s = new Scooter(); - s.setModel( "Abadah" ); - s.setSpeed( 85l ); - em.getTransaction().begin(); - em.persist( s ); - em.getTransaction().commit(); - em.close(); - em = emf.createEntityManager(); - em.getTransaction().begin(); - s = em.find( Scooter.class, s.getModel() ); - assertEquals( Long.valueOf( 85 ), s.getSpeed() ); - em.remove( s ); - em.getTransaction().commit(); - em.close(); - emf.close(); + Scooter scooter = TransactionUtil.doInJPA( () -> emf, em -> { + Scooter s = new Scooter(); + s.setModel( "Abadah" ); + s.setSpeed( 85l ); + em.persist( s ); + return s; + } ); + TransactionUtil.doInJPA( () -> emf, em -> { + Scooter s = em.find( Scooter.class, scooter.getModel() ); + assertEquals( Long.valueOf( 85 ), s.getSpeed() ); + em.remove( s ); + } ); } @Test @@ -477,22 +442,19 @@ public class PackagedEntityManagerTest extends PackagingTestCase { emf.getMetamodel().entity( Scooter.class ); // additionally, try to use them - EntityManager em = emf.createEntityManager(); - Scooter s = new Scooter(); - s.setModel( "Abadah" ); - s.setSpeed( 85l ); - em.getTransaction().begin(); - em.persist( s ); - em.getTransaction().commit(); - em.close(); - em = emf.createEntityManager(); - em.getTransaction().begin(); - s = em.find( Scooter.class, s.getModel() ); - assertEquals( Long.valueOf( 85 ), s.getSpeed() ); - em.remove( s ); - em.getTransaction().commit(); - em.close(); - emf.close(); + Scooter scooter = TransactionUtil.doInJPA( () -> emf, em -> { + Scooter s = new Scooter(); + s.setModel( "Abadah" ); + s.setSpeed( 85l ); + em.getTransaction().begin(); + em.persist( s ); + return s; + } ); + TransactionUtil.doInJPA( () -> emf, em -> { + Scooter s = em.find( Scooter.class, scooter.getModel() ); + assertEquals( Long.valueOf( 85 ), s.getSpeed() ); + em.remove( s ); + } ); } @Test @@ -501,17 +463,16 @@ public class PackagedEntityManagerTest extends PackagingTestCase { addPackageToClasspath( testPackage ); emf = Persistence.createEntityManagerFactory( "manager1", new HashMap() ); - EntityManager em = emf.createEntityManager(); - Seat seat = new Seat(); - seat.setNumber( "3B" ); - Airplane plane = new Airplane(); - plane.setSerialNumber( "75924418409052355" ); - em.getTransaction().begin(); - em.persist( seat ); - em.persist( plane ); - em.flush(); - em.getTransaction().rollback(); - em.close(); - emf.close(); + TransactionUtil.doInJPA( () -> emf, em -> { + Seat seat = new Seat(); + seat.setNumber( "3B" ); + Airplane plane = new Airplane(); + plane.setSerialNumber( "75924418409052355" ); + em.getTransaction().begin(); + em.persist( seat ); + em.persist( plane ); + em.flush(); + em.getTransaction().setRollbackOnly(); + }); } } \ No newline at end of file