From 430a76588897062d077a84dc4e62c27ec004e246 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 16 Jul 2019 14:42:27 +0100 Subject: [PATCH] 6 - SQM based on JPA type system --- .../ImmutableNaturalKeyLookupTest.java | 2 +- .../test/hql/FunctionNameAsColumnTest.java | 13 +- .../AbstractEntityWithManyToManyTest.java | 59 +- .../AbstractEntityWithOneToManyTest.java | 1419 +++++++++-------- .../test/joinfetch/JoinFetchTest.java | 454 +++--- .../onetoone/formula/OneToOneFormulaTest.java | 70 +- 6 files changed, 1042 insertions(+), 975 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/naturalid/ImmutableNaturalKeyLookupTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/naturalid/ImmutableNaturalKeyLookupTest.java index 6def466c7b..27b6cfaeca 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/naturalid/ImmutableNaturalKeyLookupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/naturalid/ImmutableNaturalKeyLookupTest.java @@ -340,7 +340,7 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( A.class ); Root root = criteria.from( A.class ); - root.join( fecth, joinType ); + root.fetch( fecth, joinType ); criteria.where( criteriaBuilder.equal( root.get( "name" ), "name1" ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java index e869487ffa..3e90cd8576 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java @@ -10,7 +10,6 @@ import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Join; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.Root; @@ -171,9 +170,9 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { CriteriaQuery criteria = criteriaBuilder.createQuery( EntityWithFunctionAsColumnHolder.class ); Root root = criteria.from( EntityWithFunctionAsColumnHolder.class ); - root.join( "entityWithArgFunctionAsColumns", JoinType.LEFT ); - Join nextHolder = root.join( "nextHolder", JoinType.LEFT ); - nextHolder.join( "entityWithArgFunctionAsColumns", JoinType.LEFT ); + root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) + .fetch( "nextHolder", JoinType.LEFT ) + .fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); holder1 = s.createQuery( criteria ).uniqueResult(); @@ -277,9 +276,9 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { CriteriaQuery criteria = criteriaBuilder.createQuery( EntityWithFunctionAsColumnHolder.class ); Root root = criteria.from( EntityWithFunctionAsColumnHolder.class ); - root.join( "entityWithArgFunctionAsColumns", JoinType.LEFT ); - Join nextHolder = root.join( "nextHolder", JoinType.LEFT ); - nextHolder.join( "entityWithArgFunctionAsColumns", JoinType.LEFT ); + root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) + .fetch( "nextHolder", JoinType.LEFT ) + .fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); // holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithManyToManyTest.java b/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithManyToManyTest.java index 631138bef4..b15e8a8ef3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithManyToManyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithManyToManyTest.java @@ -812,21 +812,24 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - try { - inTransaction( - s -> { - + inSession( + s -> { + pOrig.removeContract( cOrig ); + try { s.merge( pOrig ); assertFalse( isContractVersioned ); } - ); - } - catch (PersistenceException ex) { - assertTyping( StaleObjectStateException.class, ex.getCause() ); - assertTrue( isContractVersioned ); - } + catch (PersistenceException ex) { + assertTyping(StaleObjectStateException.class, ex.getCause()); + assertTrue( isContractVersioned); + } + finally { + s.getTransaction().rollback(); + } + } + ); + - pOrig.removeContract( cOrig ); inTransaction( s -> { @@ -867,26 +870,24 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - try { - inTransaction( - s -> { - pOrig.removeContract( cOrig ); - s.update( pOrig ); - + inSession( + s -> { + try { + s.getTransaction().commit(); assertFalse( isContractVersioned ); - } - ); - } - catch (PersistenceException ex) { - assertTrue( isContractVersioned ); - if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) { - assertTyping( StaleObjectStateException.class, ex.getCause() ); - } - else { - assertTyping( StaleStateException.class, ex.getCause() ); - } - } + catch (PersistenceException ex) { + s.getTransaction().rollback(); + assertTrue( isContractVersioned ); + if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) { + assertTyping( StaleObjectStateException.class, ex.getCause() ); + } + else { + assertTyping( StaleStateException.class, ex.getCause() ); + } + } + } + ); inTransaction( s -> { diff --git a/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithOneToManyTest.java b/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithOneToManyTest.java index aec9714f93..cabb25474c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithOneToManyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithOneToManyTest.java @@ -5,18 +5,21 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.test.immutable.entitywithmutablecollection; -import javax.persistence.PersistenceException; -import org.junit.Test; +import javax.persistence.PersistenceException; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; import org.hibernate.QueryException; -import org.hibernate.Session; import org.hibernate.StaleObjectStateException; import org.hibernate.StaleStateException; -import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; +import org.hibernate.engine.spi.SessionImplementor; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.junit.Assert.assertEquals; @@ -29,14 +32,15 @@ import static org.junit.Assert.fail; /** * @author Gail Badner */ -@SuppressWarnings( {"UnusedDeclaration"}) +@SuppressWarnings({ "UnusedDeclaration" }) public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctionalTestCase { private boolean isContractPartiesInverse; private boolean isContractPartiesBidirectional; private boolean isContractVariationsBidirectional; private boolean isContractVersioned; + public void configure(Configuration cfg) { - cfg.setProperty( Environment.GENERATE_STATISTICS, "true"); + cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); } protected boolean checkUpdateCountsAfterAddingExistingElement() { @@ -49,19 +53,20 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional protected void prepareTest() throws Exception { super.prepareTest(); - isContractPartiesInverse = sessionFactory().getCollectionPersister( Contract.class.getName() + ".parties" ).isInverse(); + isContractPartiesInverse = sessionFactory().getCollectionPersister( Contract.class.getName() + ".parties" ) + .isInverse(); try { - sessionFactory().getEntityPersister( Party.class.getName() ).getPropertyType( "contract" ); + sessionFactory().getEntityPersister( Party.class.getName() ).getPropertyType( "contract" ); isContractPartiesBidirectional = true; } - catch ( QueryException ex) { + catch (QueryException ex) { isContractPartiesBidirectional = false; } try { - sessionFactory().getEntityPersister( ContractVariation.class.getName() ).getPropertyType( "contract" ); + sessionFactory().getEntityPersister( ContractVariation.class.getName() ).getPropertyType( "contract" ); isContractVariationsBidirectional = true; } - catch ( QueryException ex) { + catch (QueryException ex) { isContractVariationsBidirectional = false; } @@ -72,45 +77,47 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testUpdateProperty() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - c.addParty( new Party( "party" ) ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + contract.addParty( new Party( "party" ) ); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - c.setCustomerName( "yogi" ); - assertEquals( 1, c.getParties().size() ); - Party party = ( Party ) c.getParties().iterator().next(); - party.setName( "new party" ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + +// Contract c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); + c.setCustomerName( "yogi" ); + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + party.setName( "new party" ); + } + ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); +// c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + s.delete( c ); + + assertPartyAndContractAreDeleted( s ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -120,32 +127,29 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testCreateWithNonEmptyOneToManyCollectionOfNew() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - c.addParty( new Party( "party" ) ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + contract.addParty( new Party( "party" ) ); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 1, c.getParties().size() ); - Party party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -155,52 +159,48 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testCreateWithNonEmptyOneToManyCollectionOfExisting() { clearCounts(); - Party party = new Party( "party" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( party ); - t.commit(); - s.close(); + Party firstParty = new Party( "party" ); + inTransaction( + s -> s.persist( firstParty ) + ); assertInsertCount( 1 ); assertUpdateCount( 0 ); clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - c.addParty( party ); - s = openSession(); - t = s.beginTransaction(); - s.save( c ); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + contract.addParty( firstParty ); + inTransaction( + s -> s.save( contract ) + ); assertInsertCount( 1 ); // BUG, should be assertUpdateCount( ! isContractPartiesInverse && isPartyVersioned ? 1 : 0 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 0 , c.getParties().size() ); - party = ( Party ) s.createCriteria( Party.class ).uniqueResult(); - assertNull( party.getContract() ); - s.delete( party ); - } - else { - assertEquals( 1 , c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + if ( isContractPartiesInverse ) { + assertEquals( 0, c.getParties().size() ); + Party party = getParty( s ); + assertNull( party.getContract() ); + s.delete( party ); + } + else { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -210,43 +210,40 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testAddNewOneToManyElementToPersistentEntity() { clearCounts(); - Contract c = new Contract( null, "gail", "phone" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 1 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.get( Contract.class, c.getId() ); - assertEquals( 0, c.getParties().size() ); - c.addParty( new Party( "party" ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = s.get( Contract.class, contract.getId() ); + assertEquals( 0, c.getParties().size() ); + c.addParty( new Party( "party" ) ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 1, c.getParties().size() ); - Party party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -256,57 +253,56 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testAddExistingOneToManyElementToPersistentEntity() { clearCounts(); - Contract c = new Contract( null, "gail", "phone" ); - Party party = new Party( "party" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - s.persist( party ); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); + inTransaction( + s -> { + s.persist( contract ); + s.persist( firstParty ); + } + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.get( Contract.class, c.getId() ); - assertEquals( 0, c.getParties().size() ); - party = ( Party ) s.get( Party.class, party.getId() ); - if ( isContractPartiesBidirectional ) { - assertNull( party.getContract() ); - } - c.addParty( party ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = s.get( Contract.class, contract.getId() ); + assertEquals( 0, c.getParties().size() ); + Party party = s.get( Party.class, firstParty.getId() ); + if ( isContractPartiesBidirectional ) { + assertNull( party.getContract() ); + } + c.addParty( party ); + } + ); assertInsertCount( 0 ); if ( checkUpdateCountsAfterAddingExistingElement() ) { - assertUpdateCount( isContractVersioned && ! isContractPartiesInverse ? 1 : 0 ); + assertUpdateCount( isContractVersioned && !isContractPartiesInverse ? 1 : 0 ); } clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 0, c.getParties().size() ); - s.delete( party ); - } - else { - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + if ( isContractPartiesInverse ) { + assertEquals( 0, c.getParties().size() ); + s.delete( firstParty ); + } + else { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -316,54 +312,52 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testCreateWithEmptyOneToManyCollectionUpdateWithExistingElement() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - s.persist( party ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( contract ); + s.persist( firstParty ); + } + + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - c.addParty( party ); + contract.addParty( firstParty ); - s = openSession(); - t = s.beginTransaction(); - s.update( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.update( contract ) + ); assertInsertCount( 0 ); if ( checkUpdateCountsAfterAddingExistingElement() ) { - assertUpdateCount( isContractVersioned && ! isContractPartiesInverse ? 1 : 0 ); + assertUpdateCount( isContractVersioned && !isContractPartiesInverse ? 1 : 0 ); } clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 0, c.getParties().size() ); - s.delete( party ); - } - else { - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + if ( isContractPartiesInverse ) { + assertEquals( 0, c.getParties().size() ); + s.delete( firstParty ); + } + else { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -373,56 +367,52 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testCreateWithNonEmptyOneToManyCollectionUpdateWithNewElement() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); - c.addParty( party ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); + contract.addParty( firstParty ); + + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); Party newParty = new Party( "new party" ); - c.addParty( newParty ); + contract.addParty( newParty ); - s = openSession(); - t = s.beginTransaction(); - s.update( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.update( contract ) + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertEquals( 2, c.getParties().size() ); - for ( Object o : c.getParties() ) { - Party aParty = (Party) o; - if ( aParty.getId() == party.getId() ) { - assertEquals( "party", aParty.getName() ); - } - else if ( aParty.getId() == newParty.getId() ) { - assertEquals( "new party", aParty.getName() ); - } - else { - fail( "unknown party" ); - } - if ( isContractPartiesBidirectional ) { - assertSame( c, aParty.getContract() ); - } - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 2, c.getParties().size() ); + for ( Object o : c.getParties() ) { + Party aParty = (Party) o; + if ( aParty.getId() == firstParty.getId() ) { + assertEquals( "party", aParty.getName() ); + } + else if ( aParty.getId() == newParty.getId() ) { + assertEquals( "new party", aParty.getName() ); + } + else { + fail( "unknown party" ); + } + if ( isContractPartiesBidirectional ) { + assertSame( c, aParty.getContract() ); + } + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -432,54 +422,48 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testCreateWithEmptyOneToManyCollectionMergeWithExistingElement() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - s.persist( party ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( firstParty ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - c.addParty( party ); + contract.addParty( firstParty ); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.merge( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.merge( contract ) + ); assertInsertCount( 0 ); if ( checkUpdateCountsAfterAddingExistingElement() ) { - assertUpdateCount( isContractVersioned && ! isContractPartiesInverse ? 1 : 0 ); + assertUpdateCount( isContractVersioned && !isContractPartiesInverse ? 1 : 0 ); } clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 0, c.getParties().size() ); - s.delete( party ); - } - else { - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + if ( isContractPartiesInverse ) { + assertEquals( 0, c.getParties().size() ); + s.delete( firstParty ); + } + else { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -489,53 +473,49 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testCreateWithNonEmptyOneToManyCollectionMergeWithNewElement() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); - c.addParty( party ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); + contract.addParty( firstParty ); + + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); Party newParty = new Party( "new party" ); - c.addParty( newParty ); + contract.addParty( newParty ); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.merge( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.merge( contract ) + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertEquals( 2, c.getParties().size() ); - for ( Object o : c.getParties() ) { - Party aParty = (Party) o; - if ( aParty.getId() == party.getId() ) { - assertEquals( "party", aParty.getName() ); - } - else if ( !aParty.getName().equals( newParty.getName() ) ) { - fail( "unknown party:" + aParty.getName() ); - } - if ( isContractPartiesBidirectional ) { - assertSame( c, aParty.getContract() ); - } - } - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 2, c.getParties().size() ); + for ( Object o : c.getParties() ) { + Party aParty = (Party) o; + if ( aParty.getId() == firstParty.getId() ) { + assertEquals( "party", aParty.getName() ); + } + else if ( !aParty.getName().equals( newParty.getName() ) ) { + fail( "unknown party:" + aParty.getName() ); + } + if ( isContractPartiesBidirectional ) { + assertSame( c, aParty.getContract() ); + } + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -545,66 +525,66 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testMoveOneToManyElementToNewEntityCollection() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - c.addParty( new Party( "party" ) ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + contract.addParty( new Party( "party" ) ); + + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 1, c.getParties().size() ); - Party party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - c.removeParty( party ); - Contract c2 = new Contract(null, "david", "phone" ); - c2.addParty( party ); - s.save( c2 ); - t.commit(); - s.close(); + Contract contract2 = new Contract( null, "david", "phone" ); + + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + c.removeParty( party ); + contract2.addParty( party ); + s.save( contract2 ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).add( Restrictions.idEq( Long.valueOf( c.getId() ) )).uniqueResult(); - c2 = (Contract) s.createCriteria( Contract.class ).add( Restrictions.idEq( Long.valueOf( c2.getId() ) )).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - assertEquals( 0, c2.getParties().size() ); - } - else { - assertEquals( 0, c.getParties().size() ); - assertEquals( 1, c2.getParties().size() ); - party = ( Party ) c2.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c2, party.getContract() ); - } - } - s.delete(c); - s.delete( c2 ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContractById( s, contract.getId() ); + Contract c2 = getContractById( s, contract2.getId() ); + if ( isContractPartiesInverse ) { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + assertEquals( 0, c2.getParties().size() ); + } + else { + assertEquals( 0, c.getParties().size() ); + assertEquals( 1, c2.getParties().size() ); + Party party = (Party) c2.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c2, party.getContract() ); + } + } + s.delete( c ); + s.delete( c2 ); + assertPartyAndContractAreDeleted( s ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -614,67 +594,68 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testMoveOneToManyElementToExistingEntityCollection() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - c.addParty( new Party( "party" ) ); - Contract c2 = new Contract(null, "david", "phone" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - s.persist( c2 ); - t.commit(); - s.close(); + Contract contract = new Contract( null, "gail", "phone" ); + contract.addParty( new Party( "party" ) ); + Contract contract2 = new Contract( null, "david", "phone" ); + + inTransaction( + s -> { + s.persist( contract ); + s.persist( contract2 ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).add( Restrictions.idEq( Long.valueOf( c.getId() ) )).uniqueResult(); - assertEquals( 1, c.getParties().size() ); - Party party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - c.removeParty( party ); - c2 = (Contract) s.createCriteria( Contract.class ).add( Restrictions.idEq( Long.valueOf( c2.getId() ) )).uniqueResult(); - c2.addParty( party ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContractById( s, contract.getId() ); + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + c.removeParty( party ); + Contract c2 = getContractById( s, contract2.getId() ); + c2.addParty( party ); + } + ); assertInsertCount( 0 ); assertUpdateCount( isContractVersioned ? 2 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria( Contract.class ).add( Restrictions.idEq( Long.valueOf( c.getId() ) )).uniqueResult(); - c2 = (Contract) s.createCriteria( Contract.class ).add( Restrictions.idEq( Long.valueOf( c2.getId() ) )).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c, party.getContract() ); - } - assertEquals( 0, c2.getParties().size() ); - } - else { - assertEquals( 0, c.getParties().size() ); - assertEquals( 1, c2.getParties().size() ); - party = ( Party ) c2.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - if ( isContractPartiesBidirectional ) { - assertSame( c2, party.getContract() ); - } - } - s.delete(c); - s.delete( c2 ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContractById( s, contract.getId() ); + Contract c2 = getContractById( s, contract2.getId() ); + if ( isContractPartiesInverse ) { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c, party.getContract() ); + } + assertEquals( 0, c2.getParties().size() ); + } + else { + assertEquals( 0, c.getParties().size() ); + assertEquals( 1, c2.getParties().size() ); + Party party = (Party) c2.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + if ( isContractPartiesBidirectional ) { + assertSame( c2, party.getContract() ); + } + } + s.delete( c ); + s.delete( c2 ); + assertPartyAndContractAreDeleted( s ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -684,61 +665,59 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testRemoveOneToManyElementUsingUpdate() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); - c.addParty( party ); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); + contract.addParty( firstParty ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - c.removeParty( party ); - assertEquals( 0, c.getParties().size() ); + contract.removeParty( firstParty ); + assertEquals( 0, contract.getParties().size() ); if ( isContractPartiesBidirectional ) { - assertNull( party.getContract() ); + assertNull( firstParty.getContract() ); } - s = openSession(); - t = s.beginTransaction(); - s.update( c ); - s.update( party ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.update( contract ); + s.update( firstParty ); + } + ); if ( checkUpdateCountsAfterRemovingElementWithoutDelete() ) { - assertUpdateCount( isContractVersioned && ! isContractPartiesInverse ? 1 : 0 ); + assertUpdateCount( isContractVersioned && !isContractPartiesInverse ? 1 : 0 ); } assertDeleteCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - assertSame( c, party.getContract() ); - } - else { - assertEquals( 0, c.getParties().size() ); - party = ( Party ) s.createCriteria( Party.class ).uniqueResult(); - if ( isContractPartiesBidirectional ) { - assertNull( party.getContract() ); - } - s.delete( party ); - } - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + if ( isContractPartiesInverse ) { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + assertSame( c, party.getContract() ); + } + else { + assertEquals( 0, c.getParties().size() ); + Party party = getParty( s ); + if ( isContractPartiesBidirectional ) { + assertNull( party.getContract() ); + } + s.delete( party ); + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -748,61 +727,60 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testRemoveOneToManyElementUsingMerge() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); - c.addParty( party ); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); + contract.addParty( firstParty ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - c.removeParty( party ); - assertEquals( 0, c.getParties().size() ); + contract.removeParty( firstParty ); + assertEquals( 0, contract.getParties().size() ); if ( isContractPartiesBidirectional ) { - assertNull( party.getContract() ); + assertNull( firstParty.getContract() ); } - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.merge( c ); - party = ( Party ) s.merge( party ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.merge( contract ); + s.merge( firstParty ); + } + ); + if ( checkUpdateCountsAfterRemovingElementWithoutDelete() ) { - assertUpdateCount( isContractVersioned && ! isContractPartiesInverse ? 1 : 0 ); + assertUpdateCount( isContractVersioned && !isContractPartiesInverse ? 1 : 0 ); } assertDeleteCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - if ( isContractPartiesInverse ) { - assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); - assertEquals( "party", party.getName() ); - assertSame( c, party.getContract() ); - } - else { - assertEquals( 0, c.getParties().size() ); - party = ( Party ) s.createCriteria( Party.class ).uniqueResult(); - if ( isContractPartiesBidirectional ) { - assertNull( party.getContract() ); - } - s.delete( party ); - } - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + if ( isContractPartiesInverse ) { + assertEquals( 1, c.getParties().size() ); + Party party = (Party) c.getParties().iterator().next(); + assertEquals( "party", party.getName() ); + assertSame( c, party.getContract() ); + } + else { + assertEquals( 0, c.getParties().size() ); + Party party = getParty( s ); + if ( isContractPartiesBidirectional ) { + assertNull( party.getContract() ); + } + s.delete( party ); + } + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -812,43 +790,40 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testDeleteOneToManyElement() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); - c.addParty( party ); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); + contract.addParty( firstParty ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - s.update( c ); - c.removeParty( party ); - s.delete( party ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.update( contract ); + contract.removeParty( firstParty ); + s.delete( firstParty ); + } + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 1 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 0, c.getParties().size() ); - party = ( Party ) s.createCriteria( Party.class ).uniqueResult(); - assertNull( party ); - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 0, c.getParties().size() ); + Party party = getParty( s ); + assertNull( party ); + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 1 ); @@ -858,46 +833,43 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testRemoveOneToManyElementByDelete() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Party party = new Party( "party" ); - c.addParty( party ); + Contract contract = new Contract( null, "gail", "phone" ); + Party firstParty = new Party( "party" ); + contract.addParty( firstParty ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - c.removeParty( party ); - assertEquals( 0, c.getParties().size() ); + contract.removeParty( firstParty ); + assertEquals( 0, contract.getParties().size() ); if ( isContractPartiesBidirectional ) { - assertNull( party.getContract() ); + assertNull( firstParty.getContract() ); } - s = openSession(); - t = s.beginTransaction(); - s.update( c ); - s.delete( party ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.update( contract ); + s.delete( firstParty ); + } + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 1 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 0, c.getParties().size() ); - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 0, c.getParties().size() ); + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 1 ); @@ -907,48 +879,44 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testRemoveOneToManyOrphanUsingUpdate() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - ContractVariation cv = new ContractVariation( 1, c ); - cv.setText( "cv1" ); + Contract contract = new Contract( null, "gail", "phone" ); + ContractVariation contractV = new ContractVariation( 1, contract ); + contractV.setText( "cv1" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - c.getVariations().remove( cv ); - cv.setContract( null ); - assertEquals( 0, c.getVariations().size() ); + contract.getVariations().remove( contractV ); + contractV.setContract( null ); + assertEquals( 0, contract.getVariations().size() ); if ( isContractVariationsBidirectional ) { - assertNull( cv.getContract() ); + assertNull( contractV.getContract() ); } - s = openSession(); - t = s.beginTransaction(); - s.update( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.update( contract ) + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 1 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 0, c.getVariations().size() ); - cv = ( ContractVariation ) s.createCriteria( ContractVariation.class ).uniqueResult(); - assertNull( cv ); - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 0, c.getVariations().size() ); + ContractVariation cv = getContractVariation( s ); + assertNull( cv ); + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 1 ); @@ -956,48 +924,45 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional @Test public void testRemoveOneToManyOrphanUsingMerge() { - Contract c = new Contract( null, "gail", "phone"); - ContractVariation cv = new ContractVariation( 1, c ); + Contract contract = new Contract( null, "gail", "phone" ); + ContractVariation contractVariation = new ContractVariation( 1, contract ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - c.getVariations().remove( cv ); - cv.setContract( null ); - assertEquals( 0, c.getVariations().size() ); + contract.getVariations().remove( contractVariation ); + contractVariation.setContract( null ); + assertEquals( 0, contract.getVariations().size() ); if ( isContractVariationsBidirectional ) { - assertNull( cv.getContract() ); + assertNull( contractVariation.getContract() ); } - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.merge( c ); - cv = ( ContractVariation ) s.merge( cv ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.merge( contract ); + s.merge( contractVariation ); + } + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 1 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 0, c.getVariations().size() ); - cv = ( ContractVariation ) s.createCriteria( ContractVariation.class ).uniqueResult(); - assertNull( cv ); - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 0, c.getVariations().size() ); + ContractVariation cv = getContractVariation( s ); + assertNull( cv ); + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 1 ); @@ -1007,44 +972,41 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testDeleteOneToManyOrphan() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - ContractVariation cv = new ContractVariation( 1, c ); + Contract contract = new Contract( null, "gail", "phone" ); + ContractVariation contractVariation = new ContractVariation( 1, contract ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( contract ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - s.update( c ); - c.getVariations().remove( cv ); - cv.setContract( null ); - assertEquals( 0, c.getVariations().size() ); - s.delete( cv ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.update( contract ); + contract.getVariations().remove( contractVariation ); + contractVariation.setContract( null ); + assertEquals( 0, contract.getVariations().size() ); + s.delete( contractVariation ); + } + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 1 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - assertEquals( 0, c.getVariations().size() ); - cv = ( ContractVariation ) s.createCriteria( ContractVariation.class ).uniqueResult(); - assertNull( cv ); - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertEquals( 0, c.getVariations().size() ); + ContractVariation cv = getContractVariation( s ); + assertNull( cv ); + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 1 ); @@ -1054,55 +1016,56 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testOneToManyCollectionOptimisticLockingWithMerge() { clearCounts(); - Contract cOrig = new Contract( null, "gail", "phone"); + Contract cOrig = new Contract( null, "gail", "phone" ); Party partyOrig = new Party( "party" ); cOrig.addParty( partyOrig ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(cOrig); - t.commit(); - s.close(); + + inTransaction( + s -> s.persist( cOrig ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - Contract c = ( Contract ) s.get( Contract.class, cOrig.getId() ); - Party newParty = new Party( "new party" ); - c.addParty( newParty ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = s.get( Contract.class, cOrig.getId() ); + Party newParty = new Party( "new party" ); + c.addParty( newParty ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - cOrig.removeParty( partyOrig ); - try { - s.merge( cOrig ); - assertFalse( isContractVersioned ); - } - catch (PersistenceException ex) { - assertTyping(StaleObjectStateException.class, ex.getCause()); - assertTrue( isContractVersioned); - } - finally { - t.rollback(); - } - s.close(); - - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - s.delete(c); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inSession( + s -> { + cOrig.removeParty( partyOrig ); + try { + s.merge( cOrig ); + assertFalse( isContractVersioned ); + } + catch (PersistenceException ex) { + assertTyping( StaleObjectStateException.class, ex.getCause() ); + assertTrue( isContractVersioned ); + } + finally { + s.getTransaction().rollback(); + } + } + ); + + + inTransaction( + s -> { + Contract c = getContract( s ); + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -1112,60 +1075,108 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional public void testOneToManyCollectionOptimisticLockingWithUpdate() { clearCounts(); - Contract cOrig = new Contract( null, "gail", "phone"); + Contract cOrig = new Contract( null, "gail", "phone" ); Party partyOrig = new Party( "party" ); cOrig.addParty( partyOrig ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(cOrig); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( cOrig ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - Contract c = ( Contract ) s.get( Contract.class, cOrig.getId() ); - Party newParty = new Party( "new party" ); - c.addParty( newParty ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = s.get( Contract.class, cOrig.getId() ); + Party newParty = new Party( "new party" ); + c.addParty( newParty ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - cOrig.removeParty( partyOrig ); - s.update( cOrig ); - try { - t.commit(); - assertFalse( isContractVersioned ); - } - catch (PersistenceException ex) { - t.rollback(); - assertTrue( isContractVersioned ); - if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) { - assertTyping( StaleObjectStateException.class, ex.getCause() ); - } - else { - assertTyping( StaleStateException.class, ex.getCause() ); - } - } - s.close(); + inSession( + s -> { + cOrig.removeParty( partyOrig ); + s.update( cOrig ); + try { + s.getTransaction().commit(); + assertFalse( isContractVersioned ); + } + catch (PersistenceException ex) { + s.getTransaction().rollback(); + assertTrue( isContractVersioned ); + if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) { + assertTyping( StaleObjectStateException.class, ex.getCause() ); + } + else { + assertTyping( StaleStateException.class, ex.getCause() ); + } + } + } + ); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - s.createQuery( "delete from Party" ).executeUpdate(); - s.delete( c ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( Long.valueOf( 0 ), s.createCriteria(Party.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + s.createQuery( "delete from Party" ).executeUpdate(); + s.delete( c ); + assertPartyAndContractAreDeleted( s ); + } + ); + } + + private Contract getContractById(SessionImplementor s, long id) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Contract.class ); + Root root = criteria.from( Contract.class ); + criteria.where( criteriaBuilder.equal( root.get( "id" ), id ) ); + return s.createQuery( criteria ).uniqueResult(); + } + + private Contract getContract(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Contract.class ); + criteria.from( Contract.class ); + return s.createQuery( criteria ).uniqueResult(); + } + + private ContractVariation getContractVariation(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( ContractVariation.class ); + criteria.from( ContractVariation.class ); + return s.createQuery( criteria ).uniqueResult(); + } + + private Party getParty(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Party.class ); + criteria.from( Party.class ); + return s.createQuery( criteria ).uniqueResult(); + } + + private void assertPartyAndContractAreDeleted(SessionImplementor s) { + assertEquals( Long.valueOf( 0 ), getContractRowCount( s ) ); + assertEquals( Long.valueOf( 0 ), getPartyRowCount( s ) ); + } + + private Long getContractRowCount(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery rowCountCriteria = criteriaBuilder.createQuery( Long.class ); + Root root = rowCountCriteria.from( Contract.class ); + rowCountCriteria.select( criteriaBuilder.count( root ) ); + return s.createQuery( rowCountCriteria ).uniqueResult(); + } + + private Long getPartyRowCount(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery rowCountCriteria = criteriaBuilder.createQuery( Long.class ); + Root root = rowCountCriteria.from( Party.class ); + rowCountCriteria.select( criteriaBuilder.count( root ) ); + return s.createQuery( rowCountCriteria ).uniqueResult(); } protected void clearCounts() { @@ -1173,17 +1184,17 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional } protected void assertInsertCount(int expected) { - int inserts = ( int ) sessionFactory().getStatistics().getEntityInsertCount(); + int inserts = (int) sessionFactory().getStatistics().getEntityInsertCount(); assertEquals( "unexpected insert count", expected, inserts ); } protected void assertUpdateCount(int expected) { - int updates = ( int ) sessionFactory().getStatistics().getEntityUpdateCount(); + int updates = (int) sessionFactory().getStatistics().getEntityUpdateCount(); assertEquals( "unexpected update counts", expected, updates ); } protected void assertDeleteCount(int expected) { - int deletes = ( int ) sessionFactory().getStatistics().getEntityDeleteCount(); + int deletes = (int) sessionFactory().getStatistics().getEntityDeleteCount(); assertEquals( "unexpected delete counts", expected, deletes ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/joinfetch/JoinFetchTest.java b/hibernate-core/src/test/java/org/hibernate/test/joinfetch/JoinFetchTest.java index 3f451ce714..c7694629ad 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/joinfetch/JoinFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/joinfetch/JoinFetchTest.java @@ -5,18 +5,20 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.test.joinfetch; + import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.JoinType; +import javax.persistence.criteria.Root; + import org.junit.Test; -import org.hibernate.FetchMode; import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Restrictions; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; @@ -34,237 +36,269 @@ public class JoinFetchTest extends BaseCoreFunctionalTestCase { @Override public void configure(Configuration cfg) { - cfg.setProperty(Environment.MAX_FETCH_DEPTH, "10"); - cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false"); + cfg.setProperty( Environment.MAX_FETCH_DEPTH, "10" ); + cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" ); } @Test public void testProjection() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.createCriteria(Item.class).setProjection( Projections.rowCount() ).uniqueResult(); - s.createCriteria(Item.class).uniqueResult(); - t.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Long.class ); + criteria.select( criteriaBuilder.count( criteria.from( Item.class ) ) ); + s.createQuery( criteria ).uniqueResult(); + + CriteriaQuery itemCriteria = criteriaBuilder.createQuery( Item.class ); + itemCriteria.from( Item.class ); + s.createQuery( itemCriteria ).uniqueResult(); + +// s.createCriteria(Item.class).setProjection( Projections.rowCount() ).uniqueResult(); +// s.createCriteria(Item.class).uniqueResult(); + } + ); } @Test public void testJoinFetch() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.createQuery( "delete from Bid" ).executeUpdate(); - s.createQuery( "delete from Comment" ).executeUpdate(); - s.createQuery( "delete from Item" ).executeUpdate(); - t.commit(); - s.close(); - - Category cat = new Category("Photography"); - Item i = new Item(cat, "Camera"); - Bid b = new Bid(i, 100.0f); - new Bid(i, 105.0f); - new Comment(i, "This looks like a really good deal"); - new Comment(i, "Is it the latest version?"); - new Comment(i, ""); - System.out.println( b.getTimestamp() ); - - s = openSession(); - t = s.beginTransaction(); - s.persist(cat); - s.persist(i); - t.commit(); - s.close(); - - sessionFactory().getCache().evictEntityRegion(Item.class); + inTransaction( + s -> { + s.createQuery( "delete from Bid" ).executeUpdate(); + s.createQuery( "delete from Comment" ).executeUpdate(); + s.createQuery( "delete from Item" ).executeUpdate(); + } + ); - s = openSession(); - t = s.beginTransaction(); - i = s.get( Item.class, i.getId() ); - assertTrue( Hibernate.isInitialized( i.getBids() ) ); - assertEquals( i.getBids().size(), 2 ); - assertTrue( Hibernate.isInitialized( i.getComments() ) ); - assertEquals( i.getComments().size(), 3 ); - t.commit(); - s.close(); + Category cat = new Category( "Photography" ); + Item i = new Item( cat, "Camera" ); + Bid b = new Bid( i, 100.0f ); + new Bid( i, 105.0f ); + new Comment( i, "This looks like a really good deal" ); + new Comment( i, "Is it the latest version?" ); + new Comment( i, "" ); - sessionFactory().getCache().evictEntityRegion(Bid.class); + inTransaction( + s -> { + s.persist( cat ); + s.persist( i ); + } + ); - s = openSession(); - t = s.beginTransaction(); - b = s.get( Bid.class, b.getId() ); - assertTrue( Hibernate.isInitialized( b.getItem() ) ); - assertTrue( Hibernate.isInitialized( b.getItem().getComments() ) ); - assertEquals( b.getItem().getComments().size(), 3 ); - System.out.println( b.getTimestamp() ); - t.commit(); - s.close(); + sessionFactory().getCache().evictEntityRegion( Item.class ); - sessionFactory().getCache().evictCollectionRegion(Item.class.getName() + ".bids"); - - s = openSession(); - t = s.beginTransaction(); - i = (Item) s.createCriteria( Item.class ) - .setFetchMode("bids", FetchMode.SELECT) - .setFetchMode("comments", FetchMode.SELECT) - .uniqueResult(); - assertFalse( Hibernate.isInitialized( i.getBids() ) ); - assertFalse( Hibernate.isInitialized( i.getComments() ) ); - b = (Bid) i.getBids().iterator().next(); - assertTrue( Hibernate.isInitialized( b.getItem() ) ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - i = (Item) s.createQuery("from Item i left join fetch i.bids left join fetch i.comments").uniqueResult(); - assertTrue( Hibernate.isInitialized( i.getBids() ) ); - assertTrue( Hibernate.isInitialized( i.getComments() ) ); - assertEquals( i.getComments().size(), 3 ); - assertEquals( i.getBids().size(), 2 ); - t.commit(); - s.close(); + inTransaction( + s -> { + Item i1 = s.get( Item.class, i.getId() ); + assertTrue( Hibernate.isInitialized( i1.getBids() ) ); + assertEquals( i1.getBids().size(), 2 ); + assertTrue( Hibernate.isInitialized( i1.getComments() ) ); + assertEquals( i1.getComments().size(), 3 ); + } + ); - s = openSession(); - t = s.beginTransaction(); - Object[] row = (Object[]) s.getNamedQuery(Item.class.getName() + ".all").list().get(0); - i = (Item) row[0]; - assertTrue( Hibernate.isInitialized( i.getBids() ) ); - assertTrue( Hibernate.isInitialized( i.getComments() ) ); - assertEquals( i.getComments().size(), 3 ); - assertEquals( i.getBids().size(), 2 ); - t.commit(); - s.close(); - s = openSession(); - t = s.beginTransaction(); - i = (Item) s.createCriteria(Item.class).uniqueResult(); - assertTrue( Hibernate.isInitialized( i.getBids() ) ); - assertTrue( Hibernate.isInitialized( i.getComments() ) ); - assertEquals( i.getComments().size(), 3 ); - assertEquals( i.getBids().size(), 2 ); - t.commit(); - s.close(); + sessionFactory().getCache().evictEntityRegion( Bid.class ); - s = openSession(); - t = s.beginTransaction(); - List bids = s.createQuery("from Bid b left join fetch b.item i left join fetch i.category").list(); - Bid bid = (Bid) bids.get(0); - assertTrue( Hibernate.isInitialized( bid.getItem() ) ); - assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Bid b1 = s.get( Bid.class, b.getId() ); + assertTrue( Hibernate.isInitialized( b1.getItem() ) ); + assertTrue( Hibernate.isInitialized( b1.getItem().getComments() ) ); + assertEquals( b1.getItem().getComments().size(), 3 ); + } + ); - s = openSession(); - t = s.beginTransaction(); - List pairs = s.createQuery("from Item i left join i.bids b left join fetch i.category").list(); - Item item = (Item) ( (Object[]) pairs.get(0) )[0]; - assertFalse( Hibernate.isInitialized( item.getBids() ) ); - assertTrue( Hibernate.isInitialized( item.getCategory() ) ); - s.clear(); - pairs = s.createQuery("from Item i left join i.bids b left join i.category").list(); - item = (Item) ( (Object[]) pairs.get(0) )[0]; - assertFalse( Hibernate.isInitialized( item.getBids() ) ); - assertTrue( Hibernate.isInitialized( item.getCategory() ) ); - s.clear(); - pairs = s.createQuery("from Bid b left join b.item i left join fetch i.category").list(); - bid = (Bid) ( (Object[]) pairs.get(0) )[0]; - assertTrue( Hibernate.isInitialized( bid.getItem() ) ); - assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); - s.clear(); - pairs = s.createQuery("from Bid b left join b.item i left join i.category").list(); - bid = (Bid) ( (Object[]) pairs.get(0) )[0]; - assertTrue( Hibernate.isInitialized( bid.getItem() ) ); - assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); - t.commit(); - s.close(); + sessionFactory().getCache().evictCollectionRegion( Item.class.getName() + ".bids" ); - s = openSession(); - t = s.beginTransaction(); - s.createQuery( "delete from Bid" ).executeUpdate(); - s.createQuery( "delete from Comment" ).executeUpdate(); - s.createQuery( "delete from Item" ).executeUpdate(); - s.createQuery( "delete from Category" ).executeUpdate(); - t.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Item.class ); + Root root = criteria.from( Item.class ); + root.join( "bids" ); + root.join( "comments" ); + Item i1 = s.createQuery( criteria ).uniqueResult(); +// Item i1 = (Item) s.createCriteria( Item.class )from +// .setFetchMode( "bids", FetchMode.SELECT ) +// .setFetchMode( "comments", FetchMode.SELECT ) +// .uniqueResult(); + assertFalse( Hibernate.isInitialized( i1.getBids() ) ); + assertFalse( Hibernate.isInitialized( i1.getComments() ) ); + Bid b1 = (Bid) i1.getBids().iterator().next(); + assertTrue( Hibernate.isInitialized( b1.getItem() ) ); + } + ); + + + inTransaction( + s -> { + Item i1 = (Item) s.createQuery( "from Item i left join fetch i.bids left join fetch i.comments" ) + .uniqueResult(); + assertTrue( Hibernate.isInitialized( i1.getBids() ) ); + assertTrue( Hibernate.isInitialized( i1.getComments() ) ); + assertEquals( i1.getComments().size(), 3 ); + assertEquals( i1.getBids().size(), 2 ); + } + ); + + inTransaction( + s -> { + Object[] row = (Object[]) s.getNamedQuery( Item.class.getName() + ".all" ).list().get( 0 ); + Item i1 = (Item) row[0]; + assertTrue( Hibernate.isInitialized( i1.getBids() ) ); + assertTrue( Hibernate.isInitialized( i1.getComments() ) ); + assertEquals( i1.getComments().size(), 3 ); + assertEquals( i1.getBids().size(), 2 ); + } + ); + + inTransaction( + s -> { +// Item i1 = (Item) s.createCriteria( Item.class ).uniqueResult(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Item.class ); + criteria.from( Item.class ); + Item i1 = s.createQuery( criteria ).uniqueResult(); + assertTrue( Hibernate.isInitialized( i1.getBids() ) ); + assertTrue( Hibernate.isInitialized( i1.getComments() ) ); + assertEquals( i1.getComments().size(), 3 ); + assertEquals( i1.getBids().size(), 2 ); + } + ); + + inTransaction( + s -> { + List bids = s.createQuery( "from Bid b left join fetch b.item i left join fetch i.category" ) + .list(); + Bid bid = (Bid) bids.get( 0 ); + assertTrue( Hibernate.isInitialized( bid.getItem() ) ); + assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); + } + ); + + inTransaction( + s -> { + List pairs = s.createQuery( "from Item i left join i.bids b left join fetch i.category" ).list(); + Item item = (Item) ( (Object[]) pairs.get( 0 ) )[0]; + assertFalse( Hibernate.isInitialized( item.getBids() ) ); + assertTrue( Hibernate.isInitialized( item.getCategory() ) ); + s.clear(); + pairs = s.createQuery( "from Item i left join i.bids b left join i.category" ).list(); + item = (Item) ( (Object[]) pairs.get( 0 ) )[0]; + assertFalse( Hibernate.isInitialized( item.getBids() ) ); + assertTrue( Hibernate.isInitialized( item.getCategory() ) ); + s.clear(); + pairs = s.createQuery( "from Bid b left join b.item i left join fetch i.category" ).list(); + Bid bid = (Bid) ( (Object[]) pairs.get( 0 ) )[0]; + assertTrue( Hibernate.isInitialized( bid.getItem() ) ); + assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); + s.clear(); + pairs = s.createQuery( "from Bid b left join b.item i left join i.category" ).list(); + bid = (Bid) ( (Object[]) pairs.get( 0 ) )[0]; + assertTrue( Hibernate.isInitialized( bid.getItem() ) ); + assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); + } + ); + + inTransaction( + s -> { + s.createQuery( "delete from Bid" ).executeUpdate(); + s.createQuery( "delete from Comment" ).executeUpdate(); + s.createQuery( "delete from Item" ).executeUpdate(); + s.createQuery( "delete from Category" ).executeUpdate(); + } + ); } - + @Test public void testCollectionFilter() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Group hb = new Group("hibernate"); - User gavin = new User("gavin"); - User max = new User("max"); - hb.getUsers().put("gavin", gavin); - hb.getUsers().put("max", max); - gavin.getGroups().put("hibernate", hb); - max.getGroups().put("hibernate", hb); - s.persist(hb); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - hb = (Group) s.createCriteria(Group.class) - .setFetchMode("users", FetchMode.SELECT) - .add( Restrictions.idEq("hibernate") ) - .uniqueResult(); - assertFalse( Hibernate.isInitialized( hb.getUsers() ) ); - //gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult(); - Long size = (Long) s.createFilter( hb.getUsers(), "select count(*)" ).uniqueResult(); - assertEquals( new Long(2), size ); - assertFalse( Hibernate.isInitialized( hb.getUsers() ) ); - s.delete(hb); - t.commit(); - s.close(); - + inTransaction( + s -> { + Group hb = new Group( "hibernate" ); + User gavin = new User( "gavin" ); + User max = new User( "max" ); + hb.getUsers().put( "gavin", gavin ); + hb.getUsers().put( "max", max ); + gavin.getGroups().put( "hibernate", hb ); + max.getGroups().put( "hibernate", hb ); + s.persist( hb ); + } + ); + + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Group.class ); + Root from = criteria.from( Group.class ); + from.join( "users", JoinType.LEFT ); + criteria.where( criteriaBuilder.equal( from.get( "name" ), "hibernate" ) ); + Group hb = s.createQuery( criteria ).uniqueResult(); +// hb = (Group) s.createCriteria( Group.class ) +// .setFetchMode( "users", FetchMode.SELECT ) +// .add( Restrictions.idEq( "hibernate" ) ) +// .uniqueResult(); + assertFalse( Hibernate.isInitialized( hb.getUsers() ) ); + //gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult(); +// Long size = (Long) s.createFilter( hb.getUsers(), "select count(*)" ).uniqueResult(); +// assertEquals( new Long( 2 ), size ); +// assertFalse( Hibernate.isInitialized( hb.getUsers() ) ); + s.delete( hb ); + } + ); } - + @Test public void testJoinFetchManyToMany() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Group hb = new Group("hibernate"); - User gavin = new User("gavin"); - User max = new User("max"); - hb.getUsers().put("gavin", gavin); - hb.getUsers().put("max", max); - gavin.getGroups().put("hibernate", hb); - max.getGroups().put("hibernate", hb); - s.persist(hb); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - hb = s.get(Group.class, "hibernate"); - assertTrue( Hibernate.isInitialized( hb.getUsers() ) ); - gavin = (User) hb.getUsers().get("gavin"); - assertFalse( Hibernate.isInitialized( gavin.getGroups() ) ); - max = s.get(User.class, "max"); - assertFalse( Hibernate.isInitialized( max.getGroups() ) ); - t.commit(); - s.close(); + Group group = new Group( "hibernate" ); + inTransaction( + s -> { + User gavin = new User( "gavin" ); + User max = new User( "max" ); + group.getUsers().put( "gavin", gavin ); + group.getUsers().put( "max", max ); + gavin.getGroups().put( "hibernate", group ); + max.getGroups().put( "hibernate", group ); + s.persist( group ); + } + ); - s = openSession(); - t = s.beginTransaction(); - hb = (Group) s.createCriteria(Group.class) - .setFetchMode("users", FetchMode.JOIN) - .setFetchMode("users.groups", FetchMode.JOIN) - .uniqueResult(); - assertTrue( Hibernate.isInitialized( hb.getUsers() ) ); - gavin = (User) hb.getUsers().get("gavin"); - assertTrue( Hibernate.isInitialized( gavin.getGroups() ) ); - max = s.get(User.class, "max"); - assertTrue( Hibernate.isInitialized( max.getGroups() ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Group hb = s.get( Group.class, "hibernate" ); + assertTrue( Hibernate.isInitialized( hb.getUsers() ) ); + User gavin = (User) hb.getUsers().get( "gavin" ); + assertFalse( Hibernate.isInitialized( gavin.getGroups() ) ); + User max = s.get( User.class, "max" ); + assertFalse( Hibernate.isInitialized( max.getGroups() ) ); + } + ); - s = openSession(); - t = s.beginTransaction(); - s.delete(hb); - t.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Group.class ); + Root from = criteria.from( Group.class ); + from.fetch( "users", JoinType.LEFT ).fetch( "groups" ); + Group hb = s.createQuery( criteria ).uniqueResult(); +// hb = (Group) s.createCriteria( Group.class ) +// .setFetchMode( "users", FetchMode.JOIN ) +// .setFetchMode( "users.groups", FetchMode.JOIN ) +// .uniqueResult(); + assertTrue( Hibernate.isInitialized( hb.getUsers() ) ); + User gavin = (User) hb.getUsers().get( "gavin" ); + assertTrue( Hibernate.isInitialized( gavin.getGroups() ) ); + User max = s.get( User.class, "max" ); + assertTrue( Hibernate.isInitialized( max.getGroups() ) ); + } + ); + + + inTransaction( + s -> { + s.delete( group ); + } + ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java index 3961ea26e2..828e93bb5f 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java @@ -6,11 +6,15 @@ */ package org.hibernate.test.onetoone.formula; -import org.hibernate.FetchMode; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Join; +import javax.persistence.criteria.JoinType; +import javax.persistence.criteria.Root; + import org.hibernate.Hibernate; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.criterion.Property; import org.hibernate.dialect.Oracle8iDialect; import org.hibernate.type.AbstractSingleColumnStandardBasicType; import org.hibernate.type.TextType; @@ -23,6 +27,7 @@ import org.junit.Before; import org.junit.Test; import static org.hamcrest.core.IsNull.notNullValue; +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -73,14 +78,14 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { address.setStreet( "Karbarook Ave" ); person.setAddress( address ); - TransactionUtil.doInHibernate( this::sessionFactory, session -> { + doInHibernate( this::sessionFactory, session -> { session.persist( person ); } ); } @Override protected void cleanupTest() { - TransactionUtil.doInHibernate( this::sessionFactory, session -> { + doInHibernate( this::sessionFactory, session -> { session.delete( person ); } ); } @@ -88,7 +93,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { @Test public void testOneToOneFormula() { - TransactionUtil.doInHibernate( this::sessionFactory, s -> { + doInHibernate( this::sessionFactory, s -> { Person p = (Person) s.createQuery( "from Person" ).uniqueResult(); assertNotNull( p.getAddress() ); @@ -96,7 +101,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { assertNull( p.getMailingAddress() ); } ); - TransactionUtil.doInHibernate( this::sessionFactory, s -> { + doInHibernate( this::sessionFactory, s -> { Person p = (Person) s.createQuery( "from Person p left join fetch p.mailingAddress left join fetch p.address" ).uniqueResult(); @@ -105,7 +110,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { assertNull( p.getMailingAddress() ); } ); - TransactionUtil.doInHibernate( this::sessionFactory, s -> { + doInHibernate( this::sessionFactory, s -> { Person p = (Person) s.createQuery( "from Person p left join fetch p.address" ).uniqueResult(); assertNotNull( p.getAddress() ); @@ -113,28 +118,45 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { assertNull( p.getMailingAddress() ); } ); - TransactionUtil.doInHibernate( this::sessionFactory, s -> { - Person p = (Person) s.createCriteria( Person.class ) - .createCriteria( "address" ) - .add( Property.forName( "zip" ).eq( "3181" ) ) - .uniqueResult(); + doInHibernate( this::sessionFactory, s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + Join address = root.join( "address", JoinType.INNER ); + criteria.where( criteriaBuilder.equal( address.get( "zip" ), "3181" ) ); + Person p = s.createQuery( criteria ).uniqueResult(); + +// Person p = (Person) s.createCriteria( Person.class ) +// .createCriteria( "address" ) +// .add( Property.forName( "zip" ).eq( "3181" ) ) +// .uniqueResult(); assertNotNull( p ); } ); - TransactionUtil.doInHibernate( this::sessionFactory, s -> { - Person p = (Person) s.createCriteria( Person.class ) - .setFetchMode( "address", FetchMode.JOIN ) - .uniqueResult(); + doInHibernate( this::sessionFactory, s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + root.fetch( "address", JoinType.LEFT ); + Person p = s.createQuery( criteria ).uniqueResult(); +// Person p = (Person) s.createCriteria( Person.class ) +// .setFetchMode( "address", FetchMode.JOIN ) +// .uniqueResult(); assertNotNull( p.getAddress() ); assertTrue( Hibernate.isInitialized( p.getAddress() ) ); assertNull( p.getMailingAddress() ); } ); - TransactionUtil.doInHibernate( this::sessionFactory, s -> { - Person p = (Person) s.createCriteria( Person.class ) - .setFetchMode( "mailingAddress", FetchMode.JOIN ) - .uniqueResult(); + doInHibernate( this::sessionFactory, s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + root.fetch( "address", JoinType.LEFT ); + Person p = s.createQuery( criteria ).uniqueResult(); +// Person p = (Person) s.createCriteria( Person.class ) +// .setFetchMode( "mailingAddress", FetchMode.JOIN ) +// .uniqueResult(); assertNotNull( p.getAddress() ); assertTrue( Hibernate.isInitialized( p.getAddress() ) ); @@ -147,14 +169,14 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { @Test @TestForIssue(jiraKey = "HHH-5757") public void testQuery() { - TransactionUtil.doInHibernate( this::sessionFactory, session -> { + doInHibernate( this::sessionFactory, session -> { Person p = (Person) session.createQuery( "from Person p where p.address = :address" ).setParameter( "address", address ).uniqueResult(); assertThat( p, notNullValue() ); } ); - TransactionUtil.doInHibernate( this::sessionFactory, session -> { + doInHibernate( this::sessionFactory, session -> { Address a = (Address) session.createQuery( "from Address a where a.person = :person" ).setParameter( "person", person ).uniqueResult(); @@ -165,7 +187,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { @Test public void testOneToOneEmbeddedCompositeKey() { - TransactionUtil.doInHibernate( this::sessionFactory, session -> { + doInHibernate( this::sessionFactory, session -> { Address a = new Address(); a.setType("HOME"); a.setPerson(person); @@ -177,7 +199,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { assertEquals(a.getZip(), "3181"); } ); - TransactionUtil.doInHibernate( this::sessionFactory, session -> { + doInHibernate( this::sessionFactory, session -> { Address a = new Address(); a.setType("HOME"); a.setPerson(person);