diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/JoinedSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/JoinedSubclassTest.java index 6db914e0b9..b61206761f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/JoinedSubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/JoinedSubclassTest.java @@ -6,15 +6,15 @@ */ package org.hibernate.test.annotations; -import org.junit.Test; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; -import org.hibernate.Session; -import org.hibernate.Transaction; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.test.annotations.inheritance.Carrot; import org.hibernate.test.annotations.inheritance.Tomato; import org.hibernate.test.annotations.inheritance.Vegetable; import org.hibernate.test.annotations.inheritance.VegetablePk; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -26,85 +26,83 @@ import static org.junit.Assert.assertTrue; public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { @Test public void testDefaultValues() { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Ferry f = new Ferry(); - f.setSize( 2 ); - f.setSea( "Channel" ); - s.persist( f ); - tx.commit(); - s.close(); + Ferry ferry = new Ferry(); + inTransaction( + s -> { + ferry.setSize( 2 ); + ferry.setSea( "Channel" ); + s.persist( ferry ); + } + ); - s = openSession(); - tx = s.beginTransaction(); - f = (Ferry) s.get( Ferry.class, f.getId() ); - assertNotNull( f ); - assertEquals( "Channel", f.getSea() ); - assertEquals( 2, f.getSize() ); - s.delete( f ); - tx.commit(); - s.close(); + inTransaction( + s -> { + Ferry f = s.get( Ferry.class, ferry.getId() ); + assertNotNull( f ); + assertEquals( "Channel", f.getSea() ); + assertEquals( 2, f.getSize() ); + s.delete( f ); + } + ); } @Test public void testDeclaredValues() { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Country c = new Country(); - c.setName( "France" ); - AmericaCupClass f = new AmericaCupClass(); - f.setSize( 2 ); - f.setCountry( c ); - s.persist( c ); - s.persist( f ); - tx.commit(); - s.close(); + Country country = new Country(); + AmericaCupClass americaCupClass = new AmericaCupClass(); + inTransaction( + s -> { + country.setName( "France" ); + americaCupClass.setSize( 2 ); + americaCupClass.setCountry( country ); + s.persist( country ); + s.persist( americaCupClass ); + } + ); - s = openSession(); - tx = s.beginTransaction(); - f = (AmericaCupClass) s.get( AmericaCupClass.class, f.getId() ); - assertNotNull( f ); - assertEquals( c, f.getCountry() ); - assertEquals( 2, f.getSize() ); - s.delete( f ); - s.delete( f.getCountry() ); - tx.commit(); - s.close(); + inTransaction( + s -> { + AmericaCupClass f = s.get( AmericaCupClass.class, americaCupClass.getId() ); + assertNotNull( f ); + assertEquals( country, f.getCountry() ); + assertEquals( 2, f.getSize() ); + s.delete( f ); + s.delete( f.getCountry() ); + } + ); } @Test - public void testCompositePk() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Carrot c = new Carrot(); - VegetablePk pk = new VegetablePk(); - pk.setFarmer( "Bill" ); - pk.setHarvestDate( "2004-08-15" ); - c.setId( pk ); - c.setLength( 23 ); - s.persist( c ); - tx.commit(); - s.close(); + public void testCompositePk() { + inTransaction( + s -> { + Carrot c = new Carrot(); + VegetablePk pk = new VegetablePk(); + pk.setFarmer( "Bill" ); + pk.setHarvestDate( "2004-08-15" ); + c.setId( pk ); + c.setLength( 23 ); + s.persist( c ); + } + ); - s = openSession(); - tx = s.beginTransaction(); - Vegetable v = (Vegetable) s.createCriteria( Vegetable.class ).uniqueResult(); - assertTrue( v instanceof Carrot ); - Carrot result = (Carrot) v; - assertEquals( 23, result.getLength() ); - tx.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Vegetable.class ); + criteria.from( Vegetable.class ); + Vegetable v = s.createQuery( criteria ).uniqueResult(); +// Vegetable v = (Vegetable) s.createCriteria( Vegetable.class ).uniqueResult(); + assertTrue( v instanceof Carrot ); + Carrot result = (Carrot) v; + assertEquals( 23, result.getLength() ); + } + ); } @Override protected Class[] getAnnotatedClasses() { - return new Class[]{ + return new Class[] { Boat.class, Ferry.class, AmericaCupClass.class, diff --git a/hibernate-core/src/test/java/org/hibernate/test/batch/BatchTest.java b/hibernate-core/src/test/java/org/hibernate/test/batch/BatchTest.java index 87c8f4ee86..81b21ec284 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/batch/BatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/batch/BatchTest.java @@ -89,7 +89,7 @@ public class BatchTest extends BaseCoreFunctionalTestCase { ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) .scroll( ScrollMode.FORWARD_ONLY ); while ( sr.next() ) { - DataPoint dp = ( DataPoint ) sr.get( 0 ); + DataPoint dp = ( DataPoint ) sr.get(); dp.setDescription( "done!" ); if ( ++i % nBeforeFlush == 0 ) { s.flush(); @@ -106,7 +106,7 @@ public class BatchTest extends BaseCoreFunctionalTestCase { sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) .scroll( ScrollMode.FORWARD_ONLY ); while ( sr.next() ) { - DataPoint dp = ( DataPoint ) sr.get( 0 ); + DataPoint dp = ( DataPoint ) sr.get(); s.delete( dp ); if ( ++i % nBeforeFlush == 0 ) { s.flush(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/cascade/circle/CascadeManagedAndTransientTest.java b/hibernate-core/src/test/java/org/hibernate/test/cascade/circle/CascadeManagedAndTransientTest.java index 023bf5f899..701d908589 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cascade/circle/CascadeManagedAndTransientTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cascade/circle/CascadeManagedAndTransientTest.java @@ -81,9 +81,9 @@ public class CascadeManagedAndTransientTest extends BaseCoreFunctionalTestCase Session s = openSession(); s.beginTransaction(); - Route route = (Route) s.createQuery("FROM Route WHERE name = :name").setString("name", "Route 1").uniqueResult(); - Node n2 = (Node) s.createQuery("FROM Node WHERE name = :name").setString("name", "Node 2").uniqueResult(); - Node n3 = (Node) s.createQuery("FROM Node WHERE name = :name").setString("name", "Node 3").uniqueResult(); + Route route = (Route) s.createQuery("FROM Route WHERE name = :name").setParameter("name", "Route 1").uniqueResult(); + Node n2 = (Node) s.createQuery("FROM Node WHERE name = :name").setParameter("name", "Node 2").uniqueResult(); + Node n3 = (Node) s.createQuery("FROM Node WHERE name = :name").setParameter("name", "Node 3").uniqueResult(); Vehicle vehicle = new Vehicle(); vehicle.setName("Bus"); diff --git a/hibernate-core/src/test/java/org/hibernate/test/connections/AggressiveReleaseTest.java b/hibernate-core/src/test/java/org/hibernate/test/connections/AggressiveReleaseTest.java index b45b291510..4436313878 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/connections/AggressiveReleaseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/connections/AggressiveReleaseTest.java @@ -140,38 +140,6 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase { done(); } - @Test - public void testQueryIteration() throws Throwable { - prepare(); - Session s = getSessionUnderTest(); - Silly silly = new Silly( "silly" ); - s.save( silly ); - s.flush(); - - Iterator itr = s.createQuery( "from Silly" ).iterate(); - assertTrue( itr.hasNext() ); - Silly silly2 = ( Silly ) itr.next(); - assertEquals( silly, silly2 ); - Hibernate.close( itr ); - - itr = s.createQuery( "from Silly" ).iterate(); - Iterator itr2 = s.createQuery( "from Silly where name = 'silly'" ).iterate(); - - assertTrue( itr.hasNext() ); - assertEquals( silly, itr.next() ); - assertTrue( itr2.hasNext() ); - assertEquals( silly, itr2.next() ); - - Hibernate.close( itr ); - Hibernate.close( itr2 ); - - s.delete( silly ); - s.flush(); - - release( s ); - done(); - } - @Test public void testQueryScrolling() throws Throwable { prepare(); @@ -182,7 +150,7 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase { ScrollableResults sr = s.createQuery( "from Silly" ).scroll(); assertTrue( sr.next() ); - Silly silly2 = ( Silly ) sr.get( 0 ); + Silly silly2 = ( Silly ) sr.get(); assertEquals( silly, silly2 ); sr.close(); @@ -190,9 +158,9 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase { ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll(); assertTrue( sr.next() ); - assertEquals( silly, sr.get( 0 ) ); + assertEquals( silly, sr.get() ); assertTrue( sr2.next() ); - assertEquals( silly, sr2.get( 0 ) ); + assertEquals( silly, sr2.get() ); sr.close(); sr2.close(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/filter/hql/JoinedFilteredBulkManipulationTest.java b/hibernate-core/src/test/java/org/hibernate/test/filter/hql/JoinedFilteredBulkManipulationTest.java index 5085110b13..a8127fcc5c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/filter/hql/JoinedFilteredBulkManipulationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/filter/hql/JoinedFilteredBulkManipulationTest.java @@ -151,8 +151,8 @@ public class JoinedFilteredBulkManipulationTest extends BaseCoreFunctionalTestCa s.beginTransaction(); s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) ); int count = s.createQuery( "update User u set u.username = :un where u.name = :n" ) - .setString( "un", "charlie" ) - .setString( "n", "Wanda" ) + .setParameter( "un", "charlie" ) + .setParameter( "n", "Wanda" ) .executeUpdate(); assertEquals( 0, count ); s.getTransaction().commit(); 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 b0d02cdce9..7315a62884 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 @@ -5,24 +5,29 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.test.immutable; -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.Hibernate; -import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.dialect.Oracle8iDialect; +import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.type.AbstractSingleColumnStandardBasicType; import org.hibernate.type.TextType; import org.hibernate.type.descriptor.sql.ClobTypeDescriptor; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.test.legacy.E; +import org.junit.Test; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -37,9 +42,11 @@ import static org.junit.Assert.fail; public class ImmutableTest extends BaseCoreFunctionalTestCase { private static class TextAsMaterializedClobType extends AbstractSingleColumnStandardBasicType { public final static TextAsMaterializedClobType INSTANCE = new TextAsMaterializedClobType(); + public TextAsMaterializedClobType() { - super( ClobTypeDescriptor.DEFAULT, TextType.INSTANCE.getJavaTypeDescriptor() ); + super( ClobTypeDescriptor.DEFAULT, TextType.INSTANCE.getJavaTypeDescriptor() ); } + public String getName() { return TextType.INSTANCE.getName(); } @@ -50,9 +57,9 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { if ( Oracle8iDialect.class.isInstance( getDialect() ) ) { cfg.registerTypeOverride( TextAsMaterializedClobType.INSTANCE ); } - cfg.setProperty( Environment.GENERATE_STATISTICS, "true"); + cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" ); - } + } @Override public String[] getMappings() { @@ -61,121 +68,160 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Test public void testChangeImmutableEntityProxyToModifiable() { - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); clearCounts(); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); + inSession( + s -> { + s.beginTransaction(); + try { + Contract c = getContract( s ); +// Contract c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); - try { - assertTrue( c instanceof HibernateProxy ); - s.setReadOnly( c, false ); - } - catch (IllegalStateException ex) { - // expected - } - finally { - t.rollback(); - s.close(); - } + assertTrue( c instanceof HibernateProxy ); + s.setReadOnly( c, false ); + } + catch (IllegalStateException ex) { + // expected + } + finally { + s.getTransaction().rollback(); + } + } + ); - s = openSession(); - t = s.beginTransaction(); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.delete( contract ); + assertEquals( + getContractRowCount( s ), + new Long( 0 ) + ); + assertEquals( + getContractVariationRowCount( s ), + new Long( 0 ) + ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); } + private Long getContractRowCount(SessionImplementor s) { + // s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Long.class ); + Root contractRoot = criteria.from( Contract.class ); + criteria.select( criteriaBuilder.count( contractRoot ) ); + return s.createQuery( criteria ).uniqueResult(); + } + + private Long getContractVariationRowCount(SessionImplementor s) { + // s.createCriteria( ContractVariation.class ).setProjection( Projections.rowCount() ).uniqueResult(), + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Long.class ); + Root contractRoot = criteria.from( ContractVariation.class ); + criteria.select( criteriaBuilder.count( contractRoot ) ); + 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(); + } + @Test public void testChangeImmutableEntityToModifiable() { - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); clearCounts(); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); + inSession( + s -> { + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); - try { - assertTrue( c instanceof HibernateProxy ); - s.setReadOnly( ( ( HibernateProxy ) c ).getHibernateLazyInitializer().getImplementation(), false ); - } - catch (IllegalStateException ex) { - // expected - } - finally { - t.rollback(); - s.close(); - } + try { + assertTrue( c instanceof HibernateProxy ); + s.setReadOnly( + ( (HibernateProxy) c ).getHibernateLazyInitializer().getImplementation(), + false + ); + } + catch (IllegalStateException ex) { + // expected + } + finally { + s.getTransaction().rollback(); + } + } + ); - s = openSession(); - t = s.beginTransaction(); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + + inTransaction( + s -> { + s.delete( contract ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractVariationRowCount( s ), new Long( 0 ) ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -183,92 +229,93 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Test public void testPersistImmutable() { - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); clearCounts(); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); - + inTransaction( + s -> { + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + s.delete( c ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractVariationRowCount( s ), new Long( 0 ) ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); } @Test public void testPersistUpdateImmutableInSameTransaction() { - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); clearCounts(); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - c.setCustomerName( "gail" ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + contract.setCustomerName( "gail" ); + + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + s.delete( c ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractVariationRowCount( s ), new Long( 0 ) ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -276,44 +323,46 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Test public void testSaveImmutable() { + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); + clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.save(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.save( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + s.delete( c ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractVariationRowCount( s ), new Long( 0 ) ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -321,44 +370,46 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Test public void testSaveOrUpdateImmutable() { + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); + clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.saveOrUpdate(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.saveOrUpdate( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + s.delete( c ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractVariationRowCount( s ), new Long( 0 ) ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -366,78 +417,78 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Test public void testRefreshImmutable() { + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); + clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.saveOrUpdate( c ); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.saveOrUpdate( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - // refresh detached - s.refresh( c ); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + // refresh detached + s.refresh( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertEquals( contract.getCustomerName(), "gavin" ); + assertEquals( contract.getVariations().size(), 2 ); + Iterator it = contract.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + } + ); assertInsertCount( 0 ); assertUpdateCount( 0 ); clearCounts(); - c.setCustomerName( "joe" ); + contract.setCustomerName( "joe" ); - s = openSession(); - t = s.beginTransaction(); + inTransaction( + s -> { + s.refresh( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertEquals( contract.getCustomerName(), "gavin" ); + assertEquals( contract.getVariations().size(), 2 ); + Iterator it = contract.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + } + ); // refresh updated detached - s.refresh( c ); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); assertInsertCount( 0 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.delete( contract ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractVariationRowCount( s ), new Long( 0 ) ); + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -445,63 +496,76 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Test public void testImmutable() { + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); + clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - c.setCustomerName("foo bar"); - cv1 = (ContractVariation) c.getVariations().iterator().next(); - cv1.setText("blah blah"); - assertTrue( s.isReadOnly( cv1 ) ); - assertFalse( s.contains( cv2 ) ); - t.commit(); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertFalse( s.contains( cv2 ) ); - s.close(); + inSession( + s -> { + try { + s.beginTransaction(); + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + c.setCustomerName( "foo bar" ); + ContractVariation cv1 = (ContractVariation) c.getVariations().iterator().next(); + cv1.setText( "blah blah" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertFalse( s.contains( contractVariation2 ) ); + s.getTransaction().commit(); + assertTrue( s.isReadOnly( c ) ); + assertTrue( s.isReadOnly( cv1 ) ); + assertFalse( s.contains( contractVariation2 ) ); + } + catch (Exception e) { + if ( s.getTransaction().isActive() ) { + s.getTransaction().rollback(); + } + throw e; + } + } + ); + assertInsertCount( 0 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + s.delete( c ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractVariationRowCount( s ), new Long( 0 ) ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -509,64 +573,77 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Test public void testPersistAndUpdateImmutable() { + Contract contract = new Contract( null, "gavin", "phone" ); + ContractVariation contractVariation1 = new ContractVariation( 1, contract ); + contractVariation1.setText( "expensive" ); + ContractVariation contractVariation2 = new ContractVariation( 2, contract ); + contractVariation2.setText( "more expensive" ); + clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.persist(c); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - c.setCustomerName( "Sherman" ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( contract ); + assertTrue( s.isReadOnly( contract ) ); + assertTrue( s.isReadOnly( contractVariation1 ) ); + assertTrue( s.isReadOnly( contractVariation2 ) ); + contract.setCustomerName( "Sherman" ); + + } + ); assertInsertCount( 3 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - c.setCustomerName("foo bar"); - cv1 = (ContractVariation) c.getVariations().iterator().next(); - cv1.setText("blah blah"); - assertTrue( s.isReadOnly( cv1 ) ); - assertFalse( s.contains( cv2 ) ); - t.commit(); - assertTrue( s.isReadOnly( c ) ); - assertTrue( s.isReadOnly( cv1 ) ); - assertFalse( s.contains( cv2 ) ); - s.close(); + inSession( + s -> { + try { + s.beginTransaction(); + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + c.setCustomerName( "foo bar" ); + ContractVariation cv1 = (ContractVariation) c.getVariations().iterator().next(); + cv1.setText( "blah blah" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertFalse( s.contains( contractVariation2 ) ); + s.getTransaction().commit(); + assertTrue( s.isReadOnly( c ) ); + assertTrue( s.isReadOnly( cv1 ) ); + assertFalse( s.contains( contractVariation2 ) ); + } + catch (Exception e) { + if ( s.getTransaction().isActive() ) { + s.getTransaction().rollback(); + } + throw e; + } + } + ); assertInsertCount( 0 ); assertUpdateCount( 0 ); clearCounts(); - s = openSession(); - t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); - assertTrue( s.isReadOnly( c ) ); - assertEquals( c.getCustomerName(), "gavin" ); - assertEquals( c.getVariations().size(), 2 ); - Iterator it = c.getVariations().iterator(); - cv1 = (ContractVariation) it.next(); - assertEquals( cv1.getText(), "expensive" ); - cv2 = (ContractVariation) it.next(); - assertEquals( cv2.getText(), "more expensive" ); - assertTrue( s.isReadOnly( cv1 ) ); - assertTrue( s.isReadOnly( cv2 ) ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - t.commit(); - s.close(); + inTransaction( + s -> { + Contract c = getContract( s ); + assertTrue( s.isReadOnly( c ) ); + assertEquals( c.getCustomerName(), "gavin" ); + assertEquals( c.getVariations().size(), 2 ); + Iterator it = c.getVariations().iterator(); + ContractVariation cv1 = (ContractVariation) it.next(); + assertEquals( cv1.getText(), "expensive" ); + ContractVariation cv2 = (ContractVariation) it.next(); + assertEquals( cv2.getText(), "more expensive" ); + assertTrue( s.isReadOnly( cv1 ) ); + assertTrue( s.isReadOnly( cv2 ) ); + s.delete( c ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + assertEquals( getContractRowCount( s ), new Long( 0 ) ); + + } + ); assertUpdateCount( 0 ); assertDeleteCount( 3 ); @@ -576,14 +653,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testUpdateAndDeleteManagedImmutable() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -593,7 +670,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertTrue( s.isReadOnly( c ) ); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); @@ -605,9 +682,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertTrue( s.isReadOnly( cv1 ) ); assertTrue( s.isReadOnly( cv2 ) ); c.setCustomerName( "Sherman" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -619,14 +701,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testGetAndDeleteManagedImmutable() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -648,9 +730,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertTrue( s.isReadOnly( cv1 ) ); assertTrue( s.isReadOnly( cv2 ) ); c.setCustomerName( "Sherman" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -662,14 +749,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testDeleteDetachedImmutable() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -680,7 +767,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); s.delete( c ); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertNull( c ); t.commit(); s.close(); @@ -693,14 +780,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testDeleteDetachedModifiedImmutable() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -716,7 +803,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s.close(); assertUpdateCount( 0 ); - assertDeleteCount( 3 ); + assertDeleteCount( 3 ); } @@ -724,14 +811,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableParentEntityWithUpdate() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -741,7 +828,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c.setCustomerName("foo bar"); + c.setCustomerName( "foo bar" ); s.update( c ); assertTrue( s.isReadOnly( c ) ); for ( Iterator it = c.getVariations().iterator(); it.hasNext(); ) { @@ -750,7 +837,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { t.commit(); assertTrue( s.isReadOnly( c ) ); for ( Iterator it = c.getVariations().iterator(); it.hasNext(); ) { - ContractVariation cv = ( ContractVariation ) it.next(); + ContractVariation cv = (ContractVariation) it.next(); assertTrue( s.contains( cv ) ); assertTrue( s.isReadOnly( cv ) ); } @@ -760,7 +847,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -768,9 +855,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -782,14 +874,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableChildEntityWithUpdate() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -800,7 +892,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); cv1 = (ContractVariation) c.getVariations().iterator().next(); - cv1.setText("blah blah"); + cv1.setText( "blah blah" ); s.update( c ); assertTrue( s.isReadOnly( c ) ); assertTrue( s.contains( cv1 ) ); @@ -815,7 +907,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -823,9 +915,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -837,14 +934,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableCollectionWithUpdate() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -853,13 +950,13 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c.getVariations().add( new ContractVariation(3, c) ); + c.getVariations().add( new ContractVariation( 3, c ) ); s.update( c ); try { t.commit(); - fail( "should have failed because reassociated object has a dirty collection"); + fail( "should have failed because reassociated object has a dirty collection" ); } - catch ( PersistenceException ex ) { + catch (PersistenceException ex) { // expected } finally { @@ -871,7 +968,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -879,9 +976,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -893,14 +995,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testUnmodifiedImmutableParentEntityWithMerge() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -910,7 +1012,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = ( Contract ) s.merge( c ); + c = (Contract) s.merge( c ); assertTrue( s.isReadOnly( c ) ); assertTrue( Hibernate.isInitialized( c.getVariations() ) ); Iterator it = c.getVariations().iterator(); @@ -925,7 +1027,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); it = c.getVariations().iterator(); @@ -933,9 +1035,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -947,14 +1054,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableParentEntityWithMerge() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -964,8 +1071,8 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c.setCustomerName("foo bar"); - c = ( Contract ) s.merge( c ); + c.setCustomerName( "foo bar" ); + c = (Contract) s.merge( c ); assertTrue( s.isReadOnly( c ) ); assertTrue( Hibernate.isInitialized( c.getVariations() ) ); Iterator it = c.getVariations().iterator(); @@ -980,7 +1087,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); it = c.getVariations().iterator(); @@ -988,9 +1095,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1002,14 +1114,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableChildEntityWithMerge() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1020,8 +1132,8 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); cv1 = (ContractVariation) c.getVariations().iterator().next(); - cv1.setText("blah blah"); - c = ( Contract ) s.merge( c ); + cv1.setText( "blah blah" ); + c = (Contract) s.merge( c ); assertTrue( s.isReadOnly( c ) ); assertTrue( Hibernate.isInitialized( c.getVariations() ) ); Iterator it = c.getVariations().iterator(); @@ -1036,7 +1148,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); it = c.getVariations().iterator(); @@ -1044,9 +1156,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1058,14 +1175,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableCollectionWithMerge() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1076,13 +1193,13 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c.getVariations().add( new ContractVariation(3, c) ); + c.getVariations().add( new ContractVariation( 3, c ) ); s.merge( c ); try { t.commit(); - fail( "should have failed because an immutable collection was changed"); + fail( "should have failed because an immutable collection was changed" ); } - catch ( PersistenceException ex ) { + catch (PersistenceException ex) { // expected t.rollback(); } @@ -1092,7 +1209,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -1100,28 +1217,33 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); assertUpdateCount( 0 ); - assertDeleteCount( 3 ); + assertDeleteCount( 3 ); } @Test public void testNewEntityViaImmutableEntityWithImmutableCollectionUsingSaveOrUpdate() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1141,19 +1263,24 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); cv1 = (ContractVariation) it.next(); assertEquals( cv1.getText(), "expensive" ); assertEquals( 1, cv1.getInfos().size() ); - assertEquals( "cv1 info", ( ( Info ) cv1.getInfos().iterator().next() ).getText() ); + assertEquals( "cv1 info", ( (Info) cv1.getInfos().iterator().next() ).getText() ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1165,14 +1292,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testNewEntityViaImmutableEntityWithImmutableCollectionUsingMerge() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1192,19 +1319,24 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); cv1 = (ContractVariation) it.next(); assertEquals( cv1.getText(), "expensive" ); assertEquals( 1, cv1.getInfos().size() ); - assertEquals( "cv1 info", ( ( Info ) cv1.getInfos().iterator().next() ).getText() ); + assertEquals( "cv1 info", ( (Info) cv1.getInfos().iterator().next() ).getText() ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1216,16 +1348,16 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testUpdatedEntityViaImmutableEntityWithImmutableCollectionUsingSaveOrUpdate() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); Info cv1Info = new Info( "cv1 info" ); cv1.getInfos().add( cv1Info ); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1246,19 +1378,24 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); cv1 = (ContractVariation) it.next(); assertEquals( cv1.getText(), "expensive" ); assertEquals( 1, cv1.getInfos().size() ); - assertEquals( "new cv1 info", ( ( Info ) cv1.getInfos().iterator().next() ).getText() ); + assertEquals( "new cv1 info", ( (Info) cv1.getInfos().iterator().next() ).getText() ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1270,16 +1407,16 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testUpdatedEntityViaImmutableEntityWithImmutableCollectionUsingMerge() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); Info cv1Info = new Info( "cv1 info" ); cv1.getInfos().add( cv1Info ); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1300,19 +1437,24 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); cv1 = (ContractVariation) it.next(); assertEquals( cv1.getText(), "expensive" ); assertEquals( 1, cv1.getInfos().size() ); - assertEquals( "new cv1 info", ( ( Info ) cv1.getInfos().iterator().next() ).getText() ); + assertEquals( "new cv1 info", ( (Info) cv1.getInfos().iterator().next() ).getText() ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1324,14 +1466,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableEntityAddImmutableToInverseMutableCollection() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); Party party = new Party( "a party" ); s.persist( party ); t.commit(); @@ -1361,7 +1503,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -1370,30 +1512,35 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); //assertEquals( 2, c.getParties().size() ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); assertUpdateCount( 0 ); assertDeleteCount( 4 ); } - + @Test public void testImmutableEntityRemoveImmutableFromInverseMutableCollection() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Party party = new Party( "party1" ); c.addParty( party ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1401,7 +1548,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertUpdateCount( 0 ); clearCounts(); - party = ( Party ) c.getParties().iterator().next(); + party = (Party) c.getParties().iterator().next(); c.removeParty( party ); s = openSession(); @@ -1415,7 +1562,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -1424,9 +1571,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); //assertEquals( 0, c.getParties().size() ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1438,16 +1590,16 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableEntityRemoveImmutableFromInverseMutableCollectionByDelete() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Party party = new Party( "party1" ); c.addParty( party ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1455,7 +1607,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertUpdateCount( 0 ); clearCounts(); - party = ( Party ) c.getParties().iterator().next(); + party = (Party) c.getParties().iterator().next(); s = openSession(); t = s.beginTransaction(); @@ -1469,7 +1621,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -1478,9 +1630,14 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); assertEquals( 0, c.getParties().size() ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1492,16 +1649,16 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { public void testImmutableEntityRemoveImmutableFromInverseMutableCollectionByDeref() { clearCounts(); - Contract c = new Contract( null, "gavin", "phone"); - ContractVariation cv1 = new ContractVariation(1, c); - cv1.setText("expensive"); - ContractVariation cv2 = new ContractVariation(2, c); - cv2.setText("more expensive"); + Contract c = new Contract( null, "gavin", "phone" ); + ContractVariation cv1 = new ContractVariation( 1, c ); + cv1.setText( "expensive" ); + ContractVariation cv2 = new ContractVariation( 2, c ); + cv2.setText( "more expensive" ); Party party = new Party( "party1" ); c.addParty( party ); Session s = openSession(); Transaction t = s.beginTransaction(); - s.persist(c); + s.persist( c ); t.commit(); s.close(); @@ -1509,7 +1666,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { assertUpdateCount( 0 ); clearCounts(); - party = ( Party ) c.getParties().iterator().next(); + party = (Party) c.getParties().iterator().next(); party.setContract( null ); s = openSession(); @@ -1520,7 +1677,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - party = ( Party ) s.get( Party.class, party.getId() ); + party = (Party) s.get( Party.class, party.getId() ); assertNotNull( party.getContract() ); t.commit(); s.close(); @@ -1530,7 +1687,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - c = (Contract) s.createCriteria(Contract.class).uniqueResult(); + c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); @@ -1539,12 +1696,17 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); assertEquals( 1, c.getParties().size() ); - party = ( Party ) c.getParties().iterator().next(); + party = (Party) c.getParties().iterator().next(); assertEquals( "party1", party.getName() ); assertSame( c, party.getContract() ); - s.delete(c); - assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); - assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) ); + s.delete( c ); + assertEquals( + s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult(), + new Long( 0 ) + ); + assertEquals( s.createCriteria( ContractVariation.class ) + .setProjection( Projections.rowCount() ) + .uniqueResult(), new Long( 0 ) ); t.commit(); s.close(); @@ -1557,17 +1719,17 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { } 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/iterate/ScrollTest.java b/hibernate-core/src/test/java/org/hibernate/test/iterate/ScrollTest.java new file mode 100644 index 0000000000..027a0d1d22 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/iterate/ScrollTest.java @@ -0,0 +1,65 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.test.iterate; + +import org.hibernate.Hibernate; +import org.hibernate.ScrollableResults; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ScrollTest extends BaseCoreFunctionalTestCase { + public String[] getMappings() { + return new String[] { "iterate/Item.hbm.xml" }; + } + + public void configure(Configuration cfg) { + super.configure( cfg ); + cfg.setProperty( Environment.USE_QUERY_CACHE, "true" ); + cfg.setProperty( Environment.CACHE_REGION_PREFIX, "foo" ); + cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" ); + cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); + } + + @Test + public void testScroll() { + inTransaction( + s -> { + Item i1 = new Item( "foo" ); + Item i2 = new Item( "bar" ); + s.persist( "Item", i1 ); + s.persist( "Item", i2 ); + } + ); + + inTransaction( + s -> { + ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll(); + assertTrue( sr.next() ); + Item i1 = (Item) sr.get(); + assertTrue( sr.next() ); + Item i2 = (Item) sr.get(); + assertTrue( Hibernate.isInitialized( i1 ) ); + assertTrue( Hibernate.isInitialized( i2 ) ); + assertEquals( i1.getName(), "foo" ); + assertEquals( i2.getName(), "bar" ); + assertFalse( sr.next() ); + s.delete( i1 ); + s.delete( i2 ); + } + ); + + assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EncapsulatedCompositeAttributeResultSetProcessorTest.java b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EncapsulatedCompositeAttributeResultSetProcessorTest.java index 80a8776f8a..195bac63a1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EncapsulatedCompositeAttributeResultSetProcessorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EncapsulatedCompositeAttributeResultSetProcessorTest.java @@ -31,6 +31,7 @@ import java.util.List; import org.hibernate.LockMode; import org.hibernate.Session; import org.hibernate.engine.spi.LoadQueryInfluencers; +import org.hibernate.engine.spi.QueryParameters; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.jdbc.Work; diff --git a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityAssociationResultSetProcessorTest.java b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityAssociationResultSetProcessorTest.java index 34cc9dd600..86f383b601 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityAssociationResultSetProcessorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityAssociationResultSetProcessorTest.java @@ -21,6 +21,7 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Session; +import org.hibernate.engine.spi.QueryParameters; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.jdbc.Work; import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor; diff --git a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManyListResultSetProcessorTest.java b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManyListResultSetProcessorTest.java index ba84cc964f..a6a2c17a5c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManyListResultSetProcessorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManyListResultSetProcessorTest.java @@ -22,6 +22,7 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Session; +import org.hibernate.engine.spi.QueryParameters; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.jdbc.Work; import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor; diff --git a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManySetResultSetProcessorTest.java b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManySetResultSetProcessorTest.java index 37c268d5a4..e77519b51f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManySetResultSetProcessorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/EntityWithNonLazyOneToManySetResultSetProcessorTest.java @@ -24,6 +24,7 @@ import java.util.Set; import org.hibernate.Hibernate; import org.hibernate.Session; +import org.hibernate.engine.spi.QueryParameters; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.jdbc.Work; import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor; diff --git a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/SimpleResultSetProcessorTest.java b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/SimpleResultSetProcessorTest.java index 69412f67fe..bfa85bfa3e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/SimpleResultSetProcessorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/loadplans/process/SimpleResultSetProcessorTest.java @@ -17,6 +17,7 @@ import java.util.List; import org.hibernate.Session; import org.hibernate.engine.jdbc.spi.JdbcServices; +import org.hibernate.engine.spi.QueryParameters; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.jdbc.Work; import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor; diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java index 33e53b6c5f..909978200f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/PropertyRefTest.java @@ -6,18 +6,15 @@ */ package org.hibernate.test.orphan; -import org.junit.Test; - -import org.hibernate.Session; -import org.hibernate.Transaction; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; /** * @author Steve Ebersole */ -@TestForIssue( jiraKey = "HHH-565" ) +@TestForIssue(jiraKey = "HHH-565") public class PropertyRefTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { @@ -26,28 +23,33 @@ public class PropertyRefTest extends BaseCoreFunctionalTestCase { @Test public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef() { - Session session = openSession(); - Transaction txn = session.beginTransaction(); User user = new User( "test" ); - user.addMail( "test" ); - user.addMail( "test" ); - session.save( user ); - txn.commit(); - session.close(); + inTransaction( + session -> { + user.addMail( "test" ); + user.addMail( "test" ); + session.save( user ); + } + ); - session = openSession(); - txn = session.beginTransaction(); - user = ( User ) session.load( User.class, user.getId() ); - session.delete( user ); - txn.commit(); - session.close(); + inTransaction( + session -> { + User u = session.load( User.class, user.getId() ); + session.delete( u ); + } + ); - session = openSession(); - txn = session.beginTransaction(); - session.createQuery( "delete from Mail where alias = :alias" ).setString( "alias", "test" ).executeUpdate(); - session.createQuery( "delete from User where userid = :userid" ).setString( "userid", "test" ).executeUpdate(); - txn.commit(); - session.close(); + inTransaction( + s -> { + session.createQuery( "delete from Mail where alias = :alias" ) + .setParameter( "alias", "test" ) + .executeUpdate(); + session.createQuery( "delete from User where userid = :userid" ) + .setParameter( "userid", "test" ) + .executeUpdate(); + + } + ); } - + } diff --git a/hibernate-core/src/test_legacy/org/hibernate/test/connections/AggressiveReleaseQueryIterationTest.java b/hibernate-core/src/test_legacy/org/hibernate/test/connections/AggressiveReleaseQueryIterationTest.java new file mode 100644 index 0000000000..a3198a364f --- /dev/null +++ b/hibernate-core/src/test_legacy/org/hibernate/test/connections/AggressiveReleaseQueryIterationTest.java @@ -0,0 +1,99 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.test.connections; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.hibernate.ConnectionReleaseMode; +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.jta.TestingJtaBootstrap; +import org.hibernate.testing.jta.TestingJtaPlatformImpl; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Andrea Boriero + */ +@RequiresDialect(H2Dialect.class) +public class AggressiveReleaseQueryIterationTest extends ConnectionManagementTestCase { + @Override + @SuppressWarnings("unchecked") + protected void addSettings(Map settings) { + super.addSettings( settings ); + + TestingJtaBootstrap.prepare( settings ); +// settings.put( Environment.TRANSACTION_STRATEGY, CMTTransactionFactory.class.getName() ); + settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class.getName() ); + settings.put( Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.AFTER_STATEMENT.toString() ); + settings.put( Environment.GENERATE_STATISTICS, "true" ); + settings.put( Environment.STATEMENT_BATCH_SIZE, "0" ); + } + + @Override + protected Session getSessionUnderTest() throws Throwable { + return openSession(); + } + + @Override + protected void reconnect(Session session) { + } + + @Override + protected void prepare() throws Throwable { + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); + } + + @Override + protected void done() throws Throwable { + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); + } + + @Test + public void testQueryIteration() throws Throwable { + prepare(); + Session s = getSessionUnderTest(); + Silly silly = new Silly( "silly" ); + s.save( silly ); + s.flush(); + + List itr = s.createQuery( "from Silly" ).iterate(); + assertTrue( itr.hasNext() ); + Silly silly2 = ( Silly ) itr.next(); + assertEquals( silly, silly2 ); + Hibernate.close( itr ); + + itr = s.createQuery( "from Silly" ).iterate(); + Iterator itr2 = s.createQuery( "from Silly where name = 'silly'" ).iterate(); + + assertTrue( itr.hasNext() ); + assertEquals( silly, itr.next() ); + assertTrue( itr2.hasNext() ); + assertEquals( silly, itr2.next() ); + + Hibernate.close( itr ); + Hibernate.close( itr2 ); + + s.delete( silly ); + s.flush(); + + release( s ); + done(); + } + + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/iterate/IterateTest.java b/hibernate-core/src/test_legacy/org/hibernate/test/iterate/IterateTest.java similarity index 72% rename from hibernate-core/src/test/java/org/hibernate/test/iterate/IterateTest.java rename to hibernate-core/src/test_legacy/org/hibernate/test/iterate/IterateTest.java index c243d855b0..5d4e1a0996 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/iterate/IterateTest.java +++ b/hibernate-core/src/test_legacy/org/hibernate/test/iterate/IterateTest.java @@ -71,35 +71,6 @@ public class IterateTest extends BaseCoreFunctionalTestCase { assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 2 ); } - @Test - public void testScroll() throws Exception { - sessionFactory().getStatistics().clear(); - Session s = openSession(); - Transaction t = s.beginTransaction(); - Item i1 = new Item("foo"); - Item i2 = new Item("bar"); - s.persist("Item", i1); - s.persist("Item", i2); - t.commit(); - s.close(); - s = openSession(); - t = s.beginTransaction(); - ScrollableResults sr = s.getNamedQuery("Item.nameDesc").scroll(); - assertTrue( sr.next() ); - i1 = (Item) sr.get(0); - assertTrue( sr.next() ); - i2 = (Item) sr.get(0); - assertTrue( Hibernate.isInitialized(i1) ); - assertTrue( Hibernate.isInitialized(i2) ); - assertEquals( i1.getName(), "foo" ); - assertEquals( i2.getName(), "bar" ); - assertFalse( sr.next() ); - s.delete(i1); - s.delete(i2); - t.commit(); - s.close(); - assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/sql/StringValueMappingTest.java b/hibernate-core/src/test_legacy/org/hibernate/test/type/descriptor/sql/StringValueMappingTest.java similarity index 100% rename from hibernate-core/src/test/java/org/hibernate/test/type/descriptor/sql/StringValueMappingTest.java rename to hibernate-core/src/test_legacy/org/hibernate/test/type/descriptor/sql/StringValueMappingTest.java