HHH-12397: commit transactions in org.hibernate.jpa.test.query.QueryTest

This commit is contained in:
Andrea Boriero 2018-03-16 16:29:43 +00:00
parent f9dc014a79
commit 1ac6218f5d
2 changed files with 59 additions and 86 deletions

View File

@ -43,6 +43,7 @@ import junit.framework.Assert;
import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -711,16 +712,14 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
final Item item = new Item( "Mouse", "Microsoft mouse" ); final Item item = new Item( "Mouse", "Microsoft mouse" );
final Item item2 = new Item( "Computer", "Dell computer" ); final Item item2 = new Item( "Computer", "Dell computer" );
EntityManager em = getOrCreateEntityManager(); doInJPA( this::entityManagerFactory, entityManager -> {
em.getTransaction().begin(); entityManager.persist( item );
try { entityManager.persist( item2 );
em.persist( item ); assertTrue( entityManager.contains( item ) );
em.persist( item2 ); } );
assertTrue( em.contains( item ) );
em.getTransaction().commit();
em.getTransaction().begin(); doInJPA( this::entityManagerFactory, entityManager -> {
Query q = em.createQuery( "select item from Item item where item.name in ?1 and item.descr = ?2" ); Query q = entityManager.createQuery( "select item from Item item where item.name in ?1 and item.descr = ?2" );
List params = new ArrayList(); List params = new ArrayList();
params.add( item.getName() ); params.add( item.getName() );
params.add( item2.getName() ); params.add( item2.getName() );
@ -729,16 +728,8 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
List result = q.getResultList(); List result = q.getResultList();
assertNotNull( result ); assertNotNull( result );
assertEquals( 1, result.size() ); assertEquals( 1, result.size() );
} } );
catch (Exception e){
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
throw e;
}
finally {
em.close();
}
} }
@Test @Test
@ -747,16 +738,15 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
final Item item = new Item( "Mouse", "Microsoft mouse" ); final Item item = new Item( "Mouse", "Microsoft mouse" );
final Item item2 = new Item( "Computer", "Dell computer" ); final Item item2 = new Item( "Computer", "Dell computer" );
EntityManager em = getOrCreateEntityManager(); doInJPA( this::entityManagerFactory, entityManager -> {
em.getTransaction().begin(); entityManager.persist( item );
try { entityManager.persist( item2 );
em.persist( item ); assertTrue( entityManager.contains( item ) );
em.persist( item2 ); } );
assertTrue( em.contains( item ) );
em.getTransaction().commit();
em.getTransaction().begin(); doInJPA( this::entityManagerFactory, entityManager -> {
Query q = em.createQuery( "select item from Item item where item.name in (?1) and item.descr = ?2" ); Query q = entityManager.createQuery(
"select item from Item item where item.name in (?1) and item.descr = ?2" );
List params = new ArrayList(); List params = new ArrayList();
params.add( item.getName() ); params.add( item.getName() );
params.add( item2.getName() ); params.add( item2.getName() );
@ -765,16 +755,7 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
List result = q.getResultList(); List result = q.getResultList();
assertNotNull( result ); assertNotNull( result );
assertEquals( 1, result.size() ); assertEquals( 1, result.size() );
} } );
catch (Exception e){
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
throw e;
}
finally {
em.close();
}
} }
@Test @Test
@ -783,16 +764,15 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
final Item item = new Item( "Mouse", "Microsoft mouse" ); final Item item = new Item( "Mouse", "Microsoft mouse" );
final Item item2 = new Item( "Computer", "Dell computer" ); final Item item2 = new Item( "Computer", "Dell computer" );
EntityManager em = getOrCreateEntityManager(); doInJPA( this::entityManagerFactory, entityManager -> {
em.getTransaction().begin(); entityManager.persist( item );
try { entityManager.persist( item2 );
em.persist( item ); assertTrue( entityManager.contains( item ) );
em.persist( item2 ); } );
assertTrue( em.contains( item ) );
em.getTransaction().commit();
em.getTransaction().begin(); doInJPA( this::entityManagerFactory, entityManager -> {
Query q = em.createQuery( "select item from Item item where item.name in (?1) and item.descr = ?2" ); Query q = entityManager.createQuery(
"select item from Item item where item.name in (?1) and item.descr = ?2" );
List params = new ArrayList(); List params = new ArrayList();
params.add( item2.getName() ); params.add( item2.getName() );
q.setParameter( 1, params ); q.setParameter( 1, params );
@ -800,16 +780,7 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
List result = q.getResultList(); List result = q.getResultList();
assertNotNull( result ); assertNotNull( result );
assertEquals( 1, result.size() ); assertEquals( 1, result.size() );
} } );
catch (Exception e){
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
throw e;
}
finally {
em.close();
}
} }
@Test @Test

View File

@ -178,15 +178,19 @@ public class JPAQLComplianceTest extends AbstractJPATest {
final Item item2 = new Item( "Computer" ); final Item item2 = new Item( "Computer" );
item2.setId( 2L ); item2.setId( 2L );
Session s = openSession(); TransactionUtil2.inTransaction(
try { sessionFactory(),
s.getTransaction().begin(); s -> {
s.save( item ); s.save( item );
s.save( item2 ); s.save( item2 );
s.getTransaction().commit(); }
);
s.getTransaction().begin(); TransactionUtil2.inTransaction(
Query q = s.createQuery( "select item from Item item where item.id in(?1) and item.name in (?2) and item.id in(?1)" ); sessionFactory(),
s -> {
Query q = s.createQuery(
"select item from Item item where item.id in(?1) and item.name in (?2) and item.id in(?1)" );
List<Long> idParams = new ArrayList<>(); List<Long> idParams = new ArrayList<>();
idParams.add( item.getId() ); idParams.add( item.getId() );
@ -202,14 +206,12 @@ public class JPAQLComplianceTest extends AbstractJPATest {
assertNotNull( result ); assertNotNull( result );
assertEquals( 2, result.size() ); assertEquals( 2, result.size() );
} }
catch (Exception e){ );
if ( s.getTransaction() != null && s.getTransaction().isActive() ) {
s.getTransaction().rollback(); TransactionUtil2.inTransaction(
} sessionFactory(),
throw e; s -> s.createQuery( "from Item" ).list().forEach( result -> s.delete( result ) )
} );
finally {
s.close();
}
} }
} }