diff --git a/hibernate-core/src/test/java/org/hibernate/test/cid/CompositeIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/cid/CompositeIdTest.java index f49b4f1a95..9f8538ff71 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/cid/CompositeIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cid/CompositeIdTest.java @@ -25,7 +25,6 @@ import org.hibernate.Transaction; import org.hibernate.dialect.Oracle8iDialect; import org.hibernate.dialect.SQLServerDialect; import org.hibernate.exception.SQLGrammarException; -import org.hibernate.hql.spi.QueryTranslator; import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; diff --git a/hibernate-core/src/test/java/org/hibernate/test/component/basic/ComponentTest.java b/hibernate-core/src/test/java/org/hibernate/test/component/basic/ComponentTest.java index a176368a68..0fbc6200b3 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/component/basic/ComponentTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/component/basic/ComponentTest.java @@ -11,13 +11,17 @@ import java.util.Date; import java.util.List; import java.util.Map; +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.Session; import org.hibernate.Transaction; import org.hibernate.boot.Metadata; import org.hibernate.cfg.Environment; -import org.hibernate.criterion.Property; -import org.hibernate.criterion.Restrictions; import org.hibernate.dialect.SybaseASE15Dialect; import org.hibernate.dialect.function.SQLFunction; import org.hibernate.mapping.Component; @@ -91,7 +95,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - u = (User) s.get(User.class, "gavin"); + u = s.get(User.class, "gavin"); assertEquals( u.getPerson().getName(), "Gavin King" ); s.delete(u); t.commit(); @@ -113,7 +117,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - u = (User) s.get(User.class, "gavin"); + u = s.get(User.class, "gavin"); assertEquals( u.getPerson().getAddress(), "Phipps Place" ); assertEquals( u.getPerson().getPreviousAddress(), "Karbarook Ave" ); assertEquals( u.getPerson().getYob(), u.getPerson().getDob().getYear()+1900 ); @@ -123,7 +127,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - u = (User) s.get(User.class, "gavin"); + u = s.get(User.class, "gavin"); assertEquals( u.getPerson().getAddress(), "Phipps Place" ); assertEquals( u.getPerson().getPreviousAddress(), "Karbarook Ave" ); assertEquals( u.getPassword(), "$ecret" ); @@ -204,14 +208,22 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery("from User u where u.person.yob = 1999").list(); - s.createCriteria(User.class) - .add( Property.forName("person.yob").between( new Integer(1999), new Integer(2002) ) ) - .list(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( User.class ); + Root root = criteria.from( User.class ); + Join person = root.join( "person", JoinType.INNER ); + criteria.where( criteriaBuilder.between( person.get( "yob" ), new Integer(1999), new Integer(2002) ) ); + s.createQuery( criteria ).list(); + +// s.createCriteria(User.class) +// .add( Property.forName("person.yob").between( new Integer(1999), new Integer(2002) ) ) +// .list(); + if ( getDialect().supportsRowValueConstructorSyntax() ) { s.createQuery("from User u where u.person = ('gavin', :dob, 'Peachtree Rd', 'Karbarook Ave', 1974, 34, 'Peachtree Rd')") - .setDate("dob", new Date("March 25, 1974")).list(); + .setParameter("dob", new Date("March 25, 1974")).list(); s.createQuery("from User where person = ('gavin', :dob, 'Peachtree Rd', 'Karbarook Ave', 1974, 34, 'Peachtree Rd')") - .setDate("dob", new Date("March 25, 1974")).list(); + .setParameter("dob", new Date("March 25, 1974")).list(); } t.commit(); s.close(); @@ -232,7 +244,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. Double heightViaSql = - ( (Number)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult()) + ( (Number)s.createNativeQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult()) .doubleValue(); assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); @@ -241,15 +253,21 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d); // Test restriction and entity load via criteria - u = (User)s.createCriteria(User.class) - .add(Restrictions.between("person.heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d)) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( User.class ); + Root root = criteria.from( User.class ); + Join person = root.join( "person", JoinType.INNER ); + criteria.where( criteriaBuilder.between( person.get( "heightInches" ), HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d) ); + u = s.createQuery( criteria ).uniqueResult(); +// u = (User)s.createCriteria(User.class) +// .add(Restrictions.between("person.heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d)) +// .uniqueResult(); assertEquals(HEIGHT_INCHES, u.getPerson().getHeightInches(), 0.01d); // Test predicate and entity load via HQL u = (User)s.createQuery("from User u where u.person.heightInches between ?1 and ?2") - .setDouble(1, HEIGHT_INCHES - 0.01d) - .setDouble(2, HEIGHT_INCHES + 0.01d) + .setParameter(1, HEIGHT_INCHES - 0.01d) + .setParameter(2, HEIGHT_INCHES + 0.01d) .uniqueResult(); assertEquals(HEIGHT_INCHES, u.getPerson().getHeightInches(), 0.01d); @@ -257,7 +275,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { u.getPerson().setHeightInches(1); s.flush(); heightViaSql = - ( (Number)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult() ) + ( (Number)s.createNativeQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult() ) .doubleValue(); assertEquals(2.54d, heightViaSql, 0.01d); s.delete(u); @@ -291,7 +309,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - emp = (Employee)s.get( Employee.class, emp.getId() ); + emp = s.get( Employee.class, emp.getId() ); t.commit(); s.close(); @@ -308,7 +326,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - emp = (Employee)s.get( Employee.class, emp.getId() ); + emp = s.get( Employee.class, emp.getId() ); t.commit(); s.close(); @@ -325,7 +343,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - emp = (Employee)s.get( Employee.class, emp.getId() ); + emp = s.get( Employee.class, emp.getId() ); Hibernate.initialize(emp.getDirectReports()); t.commit(); s.close(); @@ -347,7 +365,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - emp = (Employee)s.get( Employee.class, emp.getId() ); + emp = s.get( Employee.class, emp.getId() ); Hibernate.initialize(emp.getDirectReports()); t.commit(); s.close(); @@ -367,7 +385,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - emp = (Employee)s.get( Employee.class, emp.getId() ); + emp = s.get( Employee.class, emp.getId() ); Hibernate.initialize(emp.getDirectReports()); t.commit(); s.close(); @@ -387,7 +405,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - emp = (Employee)s.get( Employee.class, emp.getId() ); + emp = s.get( Employee.class, emp.getId() ); Hibernate.initialize(emp.getDirectReports()); t.commit(); s.close(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/flush/NativeCriteriaSyncTest.java b/hibernate-core/src/test/java/org/hibernate/test/flush/NativeCriteriaSyncTest.java index f51224c016..2cd580043a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/flush/NativeCriteriaSyncTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/flush/NativeCriteriaSyncTest.java @@ -8,7 +8,6 @@ package org.hibernate.test.flush; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import org.hibernate.criterion.Restrictions; import org.hibernate.test.hql.SimpleEntityWithAssociation; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; diff --git a/hibernate-core/src/test/java/org/hibernate/test/immutable/ImmutableTest.java b/hibernate-core/src/test/java/org/hibernate/test/immutable/ImmutableTest.java index 1b1df215d1..b0d02cdce9 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/immutable/ImmutableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/immutable/ImmutableTest.java @@ -16,7 +16,6 @@ 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.dialect.Oracle8iDialect; import org.hibernate.proxy.HibernateProxy; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; 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 91c37624b5..631138bef4 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 @@ -5,22 +5,22 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.test.immutable.entitywithmutablecollection; -import javax.persistence.PersistenceException; -import java.util.Iterator; -import org.junit.Test; +import java.util.Iterator; +import javax.persistence.PersistenceException; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; import org.hibernate.MappingException; -import org.hibernate.Session; import org.hibernate.StaleObjectStateException; import org.hibernate.StaleStateException; -import org.hibernate.Transaction; -import org.hibernate.TransactionException; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Restrictions; +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; @@ -41,19 +41,20 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona @Override public void configure(Configuration cfg) { - cfg.setProperty( Environment.GENERATE_STATISTICS, "true"); + cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); } @Override protected void prepareTest() throws Exception { super.prepareTest(); - isPlanContractsInverse = sessionFactory().getCollectionPersister( Plan.class.getName() + ".contracts" ).isInverse(); + isPlanContractsInverse = sessionFactory().getCollectionPersister( Plan.class.getName() + ".contracts" ) + .isInverse(); try { sessionFactory().getCollectionPersister( Contract.class.getName() + ".plans" ); isPlanContractsBidirectional = true; } - catch ( MappingException ex) { - isPlanContractsBidirectional = false; + catch (MappingException ex) { + isPlanContractsBidirectional = false; } isPlanVersioned = sessionFactory().getEntityPersister( Plan.class.getName() ).isVersioned(); isContractVersioned = sessionFactory().getEntityPersister( Contract.class.getName() ).isVersioned(); @@ -64,46 +65,46 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona public void testUpdateProperty() { clearCounts(); - Plan p = new Plan( "plan" ); - p.addContract( new Contract( null, "gail", "phone") ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(p); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = new Plan( "plan" ); + p.addContract( new Contract( null, "gail", "phone" ) ); + s.persist( p ); + } + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = (Plan) s.createCriteria( Plan.class ).uniqueResult(); - p.setDescription( "new plan" ); - assertEquals( 1, p.getContracts().size() ); - Contract c = ( Contract ) p.getContracts().iterator().next(); - c.setCustomerName( "yogi" ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = getPlan( s ); + p.setDescription( "new plan" ); + assertEquals( 1, p.getContracts().size() ); + Contract c = (Contract) p.getContracts().iterator().next(); + c.setCustomerName( "yogi" ); + } + ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = (Plan) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertEquals( 1, c.getPlans().size() ); - assertSame( p, c.getPlans().iterator().next() ); - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = getPlan( s ); + assertEquals( 1, p.getContracts().size() ); + Contract c = (Contract) p.getContracts().iterator().next(); + assertEquals( "gail", c.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertEquals( 1, c.getPlans().size() ); + assertSame( p, c.getPlans().iterator().next() ); + } + s.delete( p ); + + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -113,33 +114,33 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona public void testCreateWithNonEmptyManyToManyCollectionOfNew() { clearCounts(); - Plan p = new Plan( "plan" ); - p.addContract( new Contract( null, "gail", "phone") ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(p); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = new Plan( "plan" ); + p.addContract( new Contract( null, "gail", "phone" ) ); + s.persist( p ); + } + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - Contract c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertEquals( 1, c.getPlans().size() ); - assertSame( p, c.getPlans().iterator().next() ); - } - s.delete(p); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = getPlan( s ); + assertEquals( 1, p.getContracts().size() ); + Contract c = (Contract) p.getContracts().iterator().next(); + assertEquals( "gail", c.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertEquals( 1, c.getPlans().size() ); + assertSame( p, c.getPlans().iterator().next() ); + } + s.delete( p ); + + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -149,44 +150,41 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona public void testCreateWithNonEmptyManyToManyCollectionOfExisting() { clearCounts(); - Contract c = new Contract( null, "gail", "phone"); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - t.commit(); - s.close(); + Contract c = new Contract( null, "gail", "phone" ); + inTransaction( + s -> s.persist( c ) + ); assertInsertCount( 1 ); assertUpdateCount( 0 ); clearCounts(); - Plan p = new Plan( "plan" ); - p.addContract( c ); - s = openSession(); - t = s.beginTransaction(); - s.save(p); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = new Plan( "plan" ); + p.addContract( c ); + s.save( p ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertEquals( 1, c.getPlans().size() ); - assertSame( p, c.getPlans().iterator().next() ); - } - s.delete(p); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 1, p1.getContracts().size() ); + Contract c1 = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c1.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertEquals( 1, c1.getPlans().size() ); + assertSame( p1, c1.getPlans().iterator().next() ); + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -197,43 +195,42 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( p ); + } + ); assertInsertCount( 1 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.get( Plan.class, p.getId() ); - assertEquals( 0, p.getContracts().size() ); - p.addContract( new Contract( null, "gail", "phone") ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = s.get( Plan.class, p.getId() ); + assertEquals( 0, p.getContracts().size() ); + p1.addContract( new Contract( null, "gail", "phone" ) ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - Contract c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertEquals( 1, c.getPlans().size() ); - assertSame( p, c.getPlans().iterator().next() ); - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 1, p1.getContracts().size() ); + Contract c = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertEquals( 1, c.getPlans().size() ); + assertSame( p1, c.getPlans().iterator().next() ); + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -245,47 +242,46 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona Plan p = new Plan( "plan" ); Contract c = new Contract( null, "gail", "phone" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( p ); + s.persist( c ); + } + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.get( Plan.class, p.getId() ); - assertEquals( 0, p.getContracts().size() ); - c = ( Contract ) s.get( Contract.class, c.getId() ); - if ( isPlanContractsBidirectional ) { - assertEquals( 0, c.getPlans().size() ); - } - p.addContract( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = s.get( Plan.class, p.getId() ); + assertEquals( 0, p1.getContracts().size() ); + Contract c1 = s.get( Contract.class, c.getId() ); + if ( isPlanContractsBidirectional ) { + assertEquals( 0, c1.getPlans().size() ); + } + p1.addContract( c1 ); + } + ); assertInsertCount( 0 ); assertUpdateCount( isContractVersioned && isPlanVersioned ? 2 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertSame( p, c.getPlans().iterator().next() ); - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 1, p1.getContracts().size() ); + Contract c1 = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c1.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertSame( p1, c1.getPlans().iterator().next() ); + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -296,14 +292,13 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - s.persist( c ); - t.commit(); - s.close(); + Contract c = new Contract( null, "gail", "phone" ); + inTransaction( + s -> { + s.persist( p ); + s.persist( c ); + } + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -311,30 +306,27 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona p.addContract( c ); - s = openSession(); - t = s.beginTransaction(); - s.update( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.update( p ) + ); assertInsertCount( 0 ); assertUpdateCount( isContractVersioned && isPlanVersioned ? 2 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertSame( p, c.getPlans().iterator().next() ); - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 1, p1.getContracts().size() ); + Contract c1 = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c1.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertSame( p1, c1.getPlans().iterator().next() ); + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -345,13 +337,11 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(p); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -360,40 +350,37 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona Contract newC = new Contract( null, "sherman", "telepathy" ); p.addContract( newC ); - s = openSession(); - t = s.beginTransaction(); - s.update( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.update( p ) + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 2, p.getContracts().size() ); - for ( Iterator it=p.getContracts().iterator(); it.hasNext(); ) { - Contract aContract = ( Contract ) it.next(); - if ( aContract.getId() == c.getId() ) { - assertEquals( "gail", aContract.getCustomerName() ); - } - else if ( aContract.getId() == newC.getId() ) { - assertEquals( "sherman", aContract.getCustomerName() ); - } - else { - fail( "unknown contract" ); - } - if ( isPlanContractsBidirectional ) { - assertSame( p, aContract.getPlans().iterator().next() ); - } - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 2, p1.getContracts().size() ); + for ( Iterator it = p1.getContracts().iterator(); it.hasNext(); ) { + Contract aContract = (Contract) it.next(); + if ( aContract.getId() == c.getId() ) { + assertEquals( "gail", aContract.getCustomerName() ); + } + else if ( aContract.getId() == newC.getId() ) { + assertEquals( "sherman", aContract.getCustomerName() ); + } + else { + fail( "unknown contract" ); + } + if ( isPlanContractsBidirectional ) { + assertSame( p1, aContract.getPlans().iterator().next() ); + } + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -404,14 +391,14 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - s.persist( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( p ); + s.persist( c ); + } + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -419,30 +406,27 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona p.addContract( c ); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.merge( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.merge( p ) + ); assertInsertCount( 0 ); assertUpdateCount( isContractVersioned && isPlanVersioned ? 2 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertSame( p, c.getPlans().iterator().next() ); - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 1, p1.getContracts().size() ); + Contract c1 = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c1.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertSame( p1, c1.getPlans().iterator().next() ); + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -453,13 +437,11 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -468,37 +450,34 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona Contract newC = new Contract( null, "yogi", "mail" ); p.addContract( newC ); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.merge( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.merge( p ) + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned && isPlanVersioned ? 2 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 2, p.getContracts().size() ); - for ( Iterator it=p.getContracts().iterator(); it.hasNext(); ) { - Contract aContract = ( Contract ) it.next(); - if ( aContract.getId() == c.getId() ) { - assertEquals( "gail", aContract.getCustomerName() ); - } - else if ( ! aContract.getCustomerName().equals( newC.getCustomerName() ) ) { - fail( "unknown contract:" + aContract.getCustomerName() ); - } - if ( isPlanContractsBidirectional ) { - assertSame( p, aContract.getPlans().iterator().next() ); - } - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 2, p1.getContracts().size() ); + for ( Object o : p1.getContracts() ) { + Contract aContract = (Contract) o; + if ( aContract.getId() == c.getId() ) { + assertEquals( "gail", aContract.getCustomerName() ); + } + else if ( !aContract.getCustomerName().equals( newC.getCustomerName() ) ) { + fail( "unknown contract:" + aContract.getCustomerName() ); + } + if ( isPlanContractsBidirectional ) { + assertSame( p1, aContract.getPlans().iterator().next() ); + } + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -509,14 +488,12 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -527,38 +504,35 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona if ( isPlanContractsBidirectional ) { assertEquals( 0, c.getPlans().size() ); } - s = openSession(); - t = s.beginTransaction(); - s.update( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.update( p ) + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - if ( isPlanContractsInverse ) { - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - assertSame( p, c.getPlans().iterator().next() ); - } - else { - assertEquals( 0, p.getContracts().size() ); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - if ( isPlanContractsBidirectional ) { - assertEquals( 0, c.getPlans().size() ); - } - s.delete( c ); - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + if ( isPlanContractsInverse ) { + assertEquals( 1, p1.getContracts().size() ); + Contract c1 = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c1.getCustomerName() ); + assertSame( p1, c1.getPlans().iterator().next() ); + } + else { + assertEquals( 0, p1.getContracts().size() ); + Contract c1 = getContract( s ); + if ( isPlanContractsBidirectional ) { + assertEquals( 0, c1.getPlans().size() ); + } + s.delete( c1 ); + } + s.delete( p ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -569,14 +543,12 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -587,31 +559,30 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona if ( isPlanContractsBidirectional ) { assertEquals( 0, c.getPlans().size() ); } - s = openSession(); - t = s.beginTransaction(); - s.update( p ); - s.update( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.update( p ); + s.update( c ); + } + ); assertUpdateCount( isContractVersioned && isPlanVersioned ? 2 : 0 ); assertDeleteCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 0, p.getContracts().size() ); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - if ( isPlanContractsBidirectional ) { - assertEquals( 0, c.getPlans().size() ); - } - s.delete( c ); - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 0, p1.getContracts().size() ); + Contract c1 = getContract( s ); + if ( isPlanContractsBidirectional ) { + assertEquals( 0, c1.getPlans().size() ); + } + s.delete( c1 ); + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -622,14 +593,12 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -640,38 +609,36 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona if ( isPlanContractsBidirectional ) { assertEquals( 0, c.getPlans().size() ); } - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.merge( p ); - t.commit(); - s.close(); + + inTransaction( + s -> s.merge( p ) + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - if ( isPlanContractsInverse ) { - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - assertSame( p, c.getPlans().iterator().next() ); - } - else { - assertEquals( 0, p.getContracts().size() ); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - if ( isPlanContractsBidirectional ) { - assertEquals( 0, c.getPlans().size() ); - } - s.delete( c ); - } - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + if ( isPlanContractsInverse ) { + assertEquals( 1, p1.getContracts().size() ); + Contract c1 = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c1.getCustomerName() ); + assertSame( p1, c1.getPlans().iterator().next() ); + } + else { + assertEquals( 0, p1.getContracts().size() ); + Contract c1 = (Contract) getContract( s ); + if ( isPlanContractsBidirectional ) { + assertEquals( 0, c1.getPlans().size() ); + } + s.delete( c1 ); + } + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -682,14 +649,12 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -701,31 +666,30 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona assertEquals( 0, c.getPlans().size() ); } - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.merge( p ); - c = ( Contract ) s.merge( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.merge( p ); + s.merge( c ); + } + ); - assertUpdateCount( isContractVersioned && isPlanVersioned ? 2 : 0 ); + assertUpdateCount( isContractVersioned && isPlanVersioned ? 2 : 0 ); assertDeleteCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 0, p.getContracts().size() ); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - if ( isPlanContractsBidirectional ) { - assertEquals( 0, c.getPlans().size() ); - } - s.delete( c ); - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 0, p1.getContracts().size() ); + Contract c1 = getContract( s ); + if ( isPlanContractsBidirectional ) { + assertEquals( 0, c1.getPlans().size() ); + } + s.delete( c1 ); + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 2 ); @@ -736,42 +700,39 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - s.update( p ); - p.removeContract( c ); - s.delete( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.update( p ); + p.removeContract( c ); + s.delete( c ); + } + ); assertUpdateCount( isContractVersioned ? 1 : 0 ); assertDeleteCount( 1 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 0, p.getContracts().size() ); - c = ( Contract ) s.createCriteria( Contract.class ).uniqueResult(); - assertNull( c ); - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 0, p1.getContracts().size() ); + Contract c1 = getContract( s ); + assertNull( c1 ); + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 1 ); @@ -782,14 +743,12 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - Contract c = new Contract( null, "gail", "phone"); + Contract c = new Contract( null, "gail", "phone" ); p.addContract( c ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( p ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); @@ -801,26 +760,25 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona assertEquals( 0, c.getPlans().size() ); } - s = openSession(); - t = s.beginTransaction(); - s.update( p ); - s.delete( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.update( p ); + s.delete( c ); + } + ); assertUpdateCount( isPlanVersioned ? 1 : 0 ); assertDeleteCount( 1 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 0, p.getContracts().size() ); - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 0, p1.getContracts().size() ); + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 1 ); @@ -831,54 +789,52 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan pOrig = new Plan( "plan" ); - Contract cOrig = new Contract( null, "gail", "phone"); + Contract cOrig = new Contract( null, "gail", "phone" ); pOrig.addContract( cOrig ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( pOrig ); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( pOrig ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - Plan p = ( Plan ) s.get( Plan.class, pOrig.getId() ); - Contract newC = new Contract( null, "sherman", "note" ); - p.addContract( newC ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = s.get( Plan.class, pOrig.getId() ); + Contract newC = new Contract( null, "sherman", "note" ); + p.addContract( newC ); + } + ); + assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - pOrig.removeContract( cOrig ); try { - s.merge( pOrig ); - assertFalse( isContractVersioned ); + inTransaction( + s -> { + + s.merge( pOrig ); + assertFalse( isContractVersioned ); + } + ); } catch (PersistenceException ex) { - assertTyping(StaleObjectStateException.class, ex.getCause()); - assertTrue( isContractVersioned); + assertTyping( StaleObjectStateException.class, ex.getCause() ); + assertTrue( isContractVersioned ); } - finally { - t.rollback(); - } - s.close(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - s.delete( p ); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + pOrig.removeContract( cOrig ); + + inTransaction( + s -> { + Plan p1 = getPlan( s ); + s.delete( p1 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -889,40 +845,40 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan pOrig = new Plan( "plan" ); - Contract cOrig = new Contract( null, "gail", "phone"); + Contract cOrig = new Contract( null, "gail", "phone" ); pOrig.addContract( cOrig ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(pOrig); - t.commit(); - s.close(); + inTransaction( + s -> s.persist( pOrig ) + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - Plan p = ( Plan ) s.get( Plan.class, pOrig.getId() ); - Contract newC = new Contract( null, "yogi", "pawprint" ); - p.addContract( newC ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p = s.get( Plan.class, pOrig.getId() ); + Contract newC = new Contract( null, "yogi", "pawprint" ); + p.addContract( newC ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isContractVersioned ? 1 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - pOrig.removeContract( cOrig ); - s.update( pOrig ); try { - t.commit(); - assertFalse( isContractVersioned ); + inTransaction( + s -> { + pOrig.removeContract( cOrig ); + s.update( pOrig ); + + assertFalse( isContractVersioned ); + + } + ); } catch (PersistenceException ex) { - t.rollback(); assertTrue( isContractVersioned ); if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) { assertTyping( StaleObjectStateException.class, ex.getCause() ); @@ -931,17 +887,15 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona assertTyping( StaleStateException.class, ex.getCause() ); } } - s.close(); - s = openSession(); - t = s.beginTransaction(); - p = ( Plan ) s.createCriteria( Plan.class ).uniqueResult(); - s.delete( p ); - s.createQuery( "delete from Contract" ).executeUpdate(); - assertEquals( new Long( 0 ), s.createCriteria(Plan.class).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + s.delete( p1 ); + s.createQuery( "delete from Contract" ).executeUpdate(); + assertAllPlansAndContractsAreDeleted( s ); + } + ); } @Test @@ -949,67 +903,67 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - p.addContract( new Contract( null, "gail", "phone" ) ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - t.commit(); - s.close(); + inTransaction( + s -> { + p.addContract( new Contract( null, "gail", "phone" ) ); + s.persist( p ); + } + ); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = (Plan) s.createCriteria( Plan.class ).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - Contract c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertSame( p, c.getPlans().iterator().next() ); - } - p.removeContract( c ); Plan p2 = new Plan( "new plan" ); - p2.addContract( c ); - s.save( p2 ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p1 = getPlan( s ); + assertEquals( 1, p1.getContracts().size() ); + Contract c = (Contract) p1.getContracts().iterator().next(); + assertEquals( "gail", c.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertSame( p1, c.getPlans().iterator().next() ); + } + p.removeContract( c ); + + p2.addContract( c ); + s.save( p2 ); + } + ); assertInsertCount( 1 ); assertUpdateCount( isPlanVersioned && isContractVersioned ? 2 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = (Plan) s.createCriteria( Plan.class ).add( Restrictions.idEq( new Long( p.getId() ) )).uniqueResult(); - p2 = (Plan) s.createCriteria( Plan.class ).add( Restrictions.idEq( new Long( p2.getId() ) )).uniqueResult(); + inTransaction( + s -> { + Plan p1 = getPlan( s, p.getId() ); + Plan p3 = getPlan( s, p2.getId() ); /* if ( isPlanContractsInverse ) { - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); + assertEquals( 1, p1.getContracts().size() ); + c = ( Contract ) p1.getContracts().iterator().next(); assertEquals( "gail", c.getCustomerName() ); if ( isPlanContractsBidirectional ) { - assertSame( p, c.getPlans().iterator().next() ); + assertSame( p1, c.getPlans().iterator().next() ); } assertEquals( 0, p2.getContracts().size() ); } else { */ - assertEquals( 0, p.getContracts().size() ); - assertEquals( 1, p2.getContracts().size() ); - c = ( Contract ) p2.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertSame( p2, c.getPlans().iterator().next() ); - } - //} - s.delete( p ); - s.delete( p2 ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + assertEquals( 0, p1.getContracts().size() ); + assertEquals( 1, p3.getContracts().size() ); + Contract c = (Contract) p3.getContracts().iterator().next(); + assertEquals( "gail", c.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertSame( p3, c.getPlans().iterator().next() ); + } + //} + s.delete( p1 ); + s.delete( p3 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -1020,99 +974,149 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona clearCounts(); Plan p = new Plan( "plan" ); - p.addContract( new Contract( null, "gail", "phone" ) ); + Contract contract = new Contract( null, "gail", "phone" ); + p.addContract( contract ); Plan p2 = new Plan( "plan2" ); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist( p ); - s.persist( p2 ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( p ); + s.persist( p2 ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = (Plan) s.createCriteria( Plan.class ).add( Restrictions.idEq( new Long( p.getId() ) )).uniqueResult(); - assertEquals( 1, p.getContracts().size() ); - Contract c = ( Contract ) p.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertSame( p, c.getPlans().iterator().next() ); - } - p.removeContract( c ); - t.commit(); - s.close(); - assertInsertCount( 0 ); - assertUpdateCount( isPlanVersioned && isContractVersioned ? 2 : 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - p2 = (Plan) s.createCriteria( Plan.class ).add( Restrictions.idEq( new Long( p2.getId() ) )).uniqueResult(); - c = (Contract) s.createCriteria( Contract.class ).add( Restrictions.idEq( new Long( c.getId() ) )).uniqueResult(); - p2.addContract( c ); - t.commit(); - s.close(); + inTransaction( + s -> { + Plan p3 = getPlan( s, p.getId() ); + assertEquals( 1, p3.getContracts().size() ); + Contract c = (Contract) p3.getContracts().iterator().next(); + assertEquals( "gail", c.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertSame( p3, c.getPlans().iterator().next() ); + } + p3.removeContract( c ); + } + ); assertInsertCount( 0 ); assertUpdateCount( isPlanVersioned && isContractVersioned ? 2 : 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - p = (Plan) s.createCriteria( Plan.class ).add( Restrictions.idEq( new Long( p.getId() ) )).uniqueResult(); - p2 = (Plan) s.createCriteria( Plan.class ).add( Restrictions.idEq( new Long( p2.getId() ) )).uniqueResult(); + inTransaction( + s -> { + Plan p3 = getPlan( s, p2.getId() ); + Contract c1 = getContract( s, contract.getId() ); + p3.addContract( c1 ); + } + ); + + assertInsertCount( 0 ); + assertUpdateCount( isPlanVersioned && isContractVersioned ? 2 : 0 ); + clearCounts(); + + inTransaction( + s -> { + Plan p3 = getPlan( s, p.getId() ); + Plan p4 = getPlan( s, p2.getId() ); /* if ( isPlanContractsInverse ) { - assertEquals( 1, p.getContracts().size() ); - c = ( Contract ) p.getContracts().iterator().next(); + assertEquals( 1, p3.getContracts().size() ); + c = ( Contract ) p3.getContracts().iterator().next(); assertEquals( "gail", c.getCustomerName() ); if ( isPlanContractsBidirectional ) { - assertSame( p, c.getPlans().iterator().next() ); + assertSame( p3, c.getPlans().iterator().next() ); } - assertEquals( 0, p2.getContracts().size() ); + assertEquals( 0, p4.getContracts().size() ); } else { */ - assertEquals( 0, p.getContracts().size() ); - assertEquals( 1, p2.getContracts().size() ); - c = ( Contract ) p2.getContracts().iterator().next(); - assertEquals( "gail", c.getCustomerName() ); - if ( isPlanContractsBidirectional ) { - assertSame( p2, c.getPlans().iterator().next() ); - } - //} - s.delete( p ); - s.delete( p2 ); - assertEquals( new Long( 0 ), s.createCriteria( Plan.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - assertEquals( new Long( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); - t.commit(); - s.close(); + assertEquals( 0, p3.getContracts().size() ); + assertEquals( 1, p4.getContracts().size() ); + Contract c1 = (Contract) p4.getContracts().iterator().next(); + assertEquals( "gail", c1.getCustomerName() ); + if ( isPlanContractsBidirectional ) { + assertSame( p3, c1.getPlans().iterator().next() ); + } + //} + s.delete( p3 ); + s.delete( p4 ); + assertAllPlansAndContractsAreDeleted( s ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); } + private void assertAllPlansAndContractsAreDeleted(SessionImplementor s) { + assertEquals( new Long( 0 ), getPlanRowCount( s ) ); + assertEquals( new Long( 0 ), getContractRowCount( s ) ); + } + + private Plan getPlan(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Plan.class ); + criteria.from( Plan.class ); + return s.createQuery( criteria ).uniqueResult(); + } + + private Plan getPlan(SessionImplementor s, long id) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Plan.class ); + Root root = criteria.from( Plan.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 Contract getContract(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 Long getPlanRowCount(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteriaPlanRowCount = criteriaBuilder.createQuery( Long.class ); + criteriaPlanRowCount.select( criteriaBuilder.count( criteriaPlanRowCount.from( Plan.class ) ) ); + return s.createQuery( criteriaPlanRowCount ).uniqueResult(); + } + + private Long getContractRowCount(SessionImplementor s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteriaContractRowCount = criteriaBuilder.createQuery( Long.class ); + criteriaContractRowCount.select( criteriaBuilder.count( criteriaContractRowCount.from( Contract.class ) ) ); + return s.createQuery( criteriaContractRowCount ).uniqueResult(); + } + protected void clearCounts() { sessionFactory().getStatistics().clear(); } 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/immutable/entitywithmutablecollection/AbstractEntityWithOneToManyTest.java b/hibernate-core/src/test/java/org/hibernate/test/immutable/entitywithmutablecollection/AbstractEntityWithOneToManyTest.java index 37c030ec90..aec9714f93 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 @@ -14,12 +14,8 @@ import org.hibernate.Session; import org.hibernate.StaleObjectStateException; import org.hibernate.StaleStateException; import org.hibernate.Transaction; -import org.hibernate.TransactionException; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Restrictions; -import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; diff --git a/hibernate-core/src/test/java/org/hibernate/test/ops/MergeTest.java b/hibernate-core/src/test/java/org/hibernate/test/ops/MergeTest.java index 79c0a65977..00cb25ed40 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/ops/MergeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/ops/MergeTest.java @@ -18,7 +18,6 @@ import org.hibernate.NonUniqueObjectException; import org.hibernate.Session; import org.hibernate.StaleObjectStateException; import org.hibernate.Transaction; -import org.hibernate.criterion.Projections; import org.hibernate.dialect.AbstractHANADialect; import org.hibernate.testing.DialectChecks; diff --git a/hibernate-core/src/test/java/org/hibernate/test/ops/SaveOrUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/test/ops/SaveOrUpdateTest.java index ef18164914..620d5b1980 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/ops/SaveOrUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/ops/SaveOrUpdateTest.java @@ -13,7 +13,6 @@ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.criterion.Projections; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;