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 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.assertNotNull;
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 item2 = new Item( "Computer", "Dell computer" );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist( item );
em.persist( item2 );
assertTrue( em.contains( item ) );
em.getTransaction().commit();
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.persist( item );
entityManager.persist( item2 );
assertTrue( entityManager.contains( item ) );
} );
em.getTransaction().begin();
Query q = em.createQuery( "select item from Item item where item.name in ?1 and item.descr = ?2" );
doInJPA( this::entityManagerFactory, entityManager -> {
Query q = entityManager.createQuery( "select item from Item item where item.name in ?1 and item.descr = ?2" );
List params = new ArrayList();
params.add( item.getName() );
params.add( item2.getName() );
@ -729,16 +728,8 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
List result = q.getResultList();
assertNotNull( result );
assertEquals( 1, result.size() );
}
catch (Exception e){
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
throw e;
}
finally {
em.close();
}
} );
}
@Test
@ -747,16 +738,15 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
final Item item = new Item( "Mouse", "Microsoft mouse" );
final Item item2 = new Item( "Computer", "Dell computer" );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist( item );
em.persist( item2 );
assertTrue( em.contains( item ) );
em.getTransaction().commit();
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.persist( item );
entityManager.persist( item2 );
assertTrue( entityManager.contains( item ) );
} );
em.getTransaction().begin();
Query q = em.createQuery( "select item from Item item where item.name in (?1) and item.descr = ?2" );
doInJPA( this::entityManagerFactory, entityManager -> {
Query q = entityManager.createQuery(
"select item from Item item where item.name in (?1) and item.descr = ?2" );
List params = new ArrayList();
params.add( item.getName() );
params.add( item2.getName() );
@ -765,16 +755,7 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
List result = q.getResultList();
assertNotNull( result );
assertEquals( 1, result.size() );
}
catch (Exception e){
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
throw e;
}
finally {
em.close();
}
} );
}
@Test
@ -783,16 +764,15 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
final Item item = new Item( "Mouse", "Microsoft mouse" );
final Item item2 = new Item( "Computer", "Dell computer" );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist( item );
em.persist( item2 );
assertTrue( em.contains( item ) );
em.getTransaction().commit();
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.persist( item );
entityManager.persist( item2 );
assertTrue( entityManager.contains( item ) );
} );
em.getTransaction().begin();
Query q = em.createQuery( "select item from Item item where item.name in (?1) and item.descr = ?2" );
doInJPA( this::entityManagerFactory, entityManager -> {
Query q = entityManager.createQuery(
"select item from Item item where item.name in (?1) and item.descr = ?2" );
List params = new ArrayList();
params.add( item2.getName() );
q.setParameter( 1, params );
@ -800,16 +780,7 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
List result = q.getResultList();
assertNotNull( result );
assertEquals( 1, result.size() );
}
catch (Exception e){
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
throw e;
}
finally {
em.close();
}
} );
}
@Test

View File

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