diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java index 9c311e7c60..a4210d1f0f 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java @@ -590,9 +590,6 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces final PersistenceContext persistenceContext = session.getPersistenceContext(); if ( entityInstance instanceof HibernateProxy ) { LazyInitializer hibernateLazyInitializer = ( (HibernateProxy) entityInstance ).getHibernateLazyInitializer(); - if ( !hibernateLazyInitializer.isUninitialized() ) { - return; - } Object instance = persistenceContext.getEntity( entityKey ); if ( instance == null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardRowReader.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardRowReader.java index 0a62a97c03..75e86cc76a 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardRowReader.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardRowReader.java @@ -127,26 +127,6 @@ public class StandardRowReader implements RowReader { // todo (6.0) : we may want to split handling of initializers into specific sub-type handling // - meaning we'd have something like: -// for ( EntityInitializer initializer : entityInitializers ) { -// initializer.resolveKey( rowProcessingState ); -// } -// -// for ( EntityInitializer initializer : collectionInitializers ) { -// initializer.resolveKey( rowProcessingState ); -// } -// -// for ( Initializer initializer : entityInitializers ) { -// initializer.resolveInstance( rowProcessingState ); -// } -// -// for ( EntityInitializer initializer : collectionInitializers ) { -// initializer.resolveInstance( rowProcessingState ); -// } - - - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // old - final int numberOfInitializers = initializers.size(); for ( int i = 0; i < numberOfInitializers; i++ ) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyProxyTest.java similarity index 58% rename from hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyProxyTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyProxyTest.java index 567a09b69d..97860b5000 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyProxyTest.java @@ -4,50 +4,56 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.readonly; +package org.hibernate.orm.test.readonly; import java.math.BigDecimal; -import org.junit.Test; - import org.hibernate.CacheMode; import org.hibernate.Hibernate; import org.hibernate.Session; -import org.hibernate.SessionException; import org.hibernate.Transaction; import org.hibernate.TransientObjectException; import org.hibernate.UnresolvableObjectException; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.LazyInitializer; -import org.hibernate.testing.FailureExpected; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.test.readonly.AbstractReadOnlyTest; +import org.hibernate.test.readonly.DataPoint; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + /** * Tests making initialized and uninitialized proxies read-only/modifiable - * + * * @author Gail Badner */ +@DomainModel( + xmlMappings = { + "org/hibernate/test/readonly/DataPoint.hbm.xml", + "org/hibernate/test/readonly/TextHolder.hbm.xml" + } +) public class ReadOnlyProxyTest extends AbstractReadOnlyTest { - @Override - public String[] getMappings() { - return new String[] { "readonly/DataPoint.hbm.xml", "readonly/TextHolder.hbm.xml" }; - } @Test - public void testReadOnlyViaSessionDoesNotInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaSessionDoesNotInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); assertFalse( Hibernate.isInitialized( dp ) ); @@ -65,9 +71,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { assertFalse( Hibernate.isInitialized( dp ) ); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -78,15 +84,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaLazyInitializerDoesNotInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaLazyInitializerDoesNotInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - LazyInitializer dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + LazyInitializer dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); checkReadOnly( s, dp, false ); assertFalse( Hibernate.isInitialized( dp ) ); dpLI.setReadOnly( true ); @@ -103,9 +109,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { assertFalse( Hibernate.isInitialized( dp ) ); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -116,13 +122,13 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaSessionNoChangeAfterInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaSessionNoChangeAfterInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); assertFalse( Hibernate.isInitialized( dp ) ); @@ -132,10 +138,10 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); s.setReadOnly( dp, true ); checkReadOnly( s, dp, true ); @@ -146,10 +152,10 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); s.setReadOnly( dp, true ); checkReadOnly( s, dp, true ); @@ -163,9 +169,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -176,15 +182,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaLazyInitializerNoChangeAfterInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaLazyInitializerNoChangeAfterInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - LazyInitializer dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + LazyInitializer dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); checkReadOnly( s, dp, false ); assertTrue( dpLI.isUninitialized() ); Hibernate.initialize( dp ); @@ -193,12 +199,12 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); dpLI.setReadOnly( true ); checkReadOnly( s, dp, true ); assertTrue( dpLI.isUninitialized() ); @@ -208,12 +214,12 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); dpLI.setReadOnly( true ); checkReadOnly( s, dp, true ); assertTrue( dpLI.isUninitialized() ); @@ -226,9 +232,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -239,13 +245,13 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaSessionBeforeInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaSessionBeforeInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); s.setReadOnly( dp, true ); dp.setDescription( "changed" ); @@ -256,9 +262,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -269,13 +275,13 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testModifiableViaSessionBeforeInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testModifiableViaSessionBeforeInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); dp.setDescription( "changed" ); @@ -286,9 +292,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -299,13 +305,13 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaSessionBeforeInitByModifiableQuery() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaSessionBeforeInitByModifiableQuery(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -313,7 +319,8 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, true ); assertFalse( Hibernate.isInitialized( dp ) ); - DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( false ).uniqueResult(); + DataPoint dpFromQuery = (DataPoint) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( + false ).uniqueResult(); assertTrue( Hibernate.isInitialized( dpFromQuery ) ); assertSame( dp, dpFromQuery ); checkReadOnly( s, dp, true ); @@ -323,9 +330,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -336,19 +343,20 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaSessionBeforeInitByReadOnlyQuery() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaSessionBeforeInitByReadOnlyQuery(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); s.setReadOnly( dp, true ); checkReadOnly( s, dp, true ); - DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( true ).uniqueResult(); + DataPoint dpFromQuery = (DataPoint) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( + true ).uniqueResult(); assertTrue( Hibernate.isInitialized( dpFromQuery ) ); assertSame( dp, dpFromQuery ); checkReadOnly( s, dp, true ); @@ -358,9 +366,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -371,17 +379,18 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testModifiableViaSessionBeforeInitByModifiableQuery() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testModifiableViaSessionBeforeInitByModifiableQuery(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); - DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( false ).uniqueResult(); + DataPoint dpFromQuery = (DataPoint) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( + false ).uniqueResult(); assertTrue( Hibernate.isInitialized( dpFromQuery ) ); assertSame( dp, dpFromQuery ); checkReadOnly( s, dp, false ); @@ -391,9 +400,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -404,17 +413,18 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testModifiableViaSessionBeforeInitByReadOnlyQuery() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testModifiableViaSessionBeforeInitByReadOnlyQuery(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); assertFalse( Hibernate.isInitialized( dp ) ); - DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( true ).uniqueResult(); + DataPoint dpFromQuery = (DataPoint) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( + true ).uniqueResult(); assertTrue( Hibernate.isInitialized( dpFromQuery ) ); assertSame( dp, dpFromQuery ); checkReadOnly( s, dp, false ); @@ -424,9 +434,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -437,15 +447,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaLazyInitializerBeforeInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaLazyInitializerBeforeInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - LazyInitializer dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + LazyInitializer dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); assertTrue( dpLI.isUninitialized() ); checkReadOnly( s, dp, false ); dpLI.setReadOnly( true ); @@ -458,9 +468,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -471,15 +481,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testModifiableViaLazyInitializerBeforeInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testModifiableViaLazyInitializerBeforeInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - LazyInitializer dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + LazyInitializer dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); assertTrue( dp instanceof HibernateProxy ); assertTrue( dpLI.isUninitialized() ); checkReadOnly( s, dp, false ); @@ -491,9 +501,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -504,15 +514,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyViaLazyInitializerAfterInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyViaLazyInitializerAfterInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - LazyInitializer dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + LazyInitializer dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); assertTrue( dpLI.isUninitialized() ); checkReadOnly( s, dp, false ); dp.setDescription( "changed" ); @@ -525,9 +535,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -538,15 +548,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testModifiableViaLazyInitializerAfterInit() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testModifiableViaLazyInitializerAfterInit(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - LazyInitializer dpLI = ( ( HibernateProxy ) dp ).getHibernateLazyInitializer(); + LazyInitializer dpLI = ( (HibernateProxy) dp ).getHibernateLazyInitializer(); assertTrue( dpLI.isUninitialized() ); checkReadOnly( s, dp, false ); dp.setDescription( "changed" ); @@ -557,9 +567,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -570,22 +580,22 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - @FailureExpected( jiraKey = "HHH-4642" ) - public void testModifyToReadOnlyToModifiableIsUpdated() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + @FailureExpected(jiraKey = "HHH-4642") + public void testModifyToReadOnlyToModifiableIsUpdated(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); dp.setDescription( "changed" ); assertTrue( Hibernate.isInitialized( dp ) ); assertEquals( "changed", dp.getDescription() ); s.setReadOnly( dp, true ); - checkReadOnly( s, dp,true ); + checkReadOnly( s, dp, true ); s.setReadOnly( dp, false ); checkReadOnly( s, dp, false ); assertEquals( "changed", dp.getDescription() ); @@ -593,9 +603,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -607,53 +617,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { finally { s.getTransaction().rollback(); s.close(); - s = openSession(); - s.beginTransaction(); - s.delete( dp ); - s.getTransaction().commit(); - s.close(); - } - } - - @Test - @FailureExpected( jiraKey = "HHH-4642" ) - public void testReadOnlyModifiedToModifiableIsUpdated() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); - - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); - assertTrue( dp instanceof HibernateProxy ); - assertFalse( Hibernate.isInitialized( dp )); - checkReadOnly( s, dp, false ); - s.setReadOnly( dp, true ); - checkReadOnly( s, dp,true ); - dp.setDescription( "changed" ); - assertTrue( Hibernate.isInitialized( dp ) ); - assertEquals( "changed", dp.getDescription() ); - s.setReadOnly( dp, false ); - checkReadOnly( s, dp, false ); - assertEquals( "changed", dp.getDescription() ); - s.flush(); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); - assertEquals( dpOrig.getId(), dp.getId() ); - assertEquals( dpOrig.getDescription(), dp.getDescription() ); - assertEquals( dpOrig.getX(), dp.getX() ); - assertEquals( dpOrig.getY(), dp.getY() ); - try { - assertEquals( "changed", dp.getDescription() ); - // should fail due to HHH-4642 - } - finally { - s.getTransaction().rollback(); - s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); @@ -662,18 +626,64 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyChangedEvictedUpdate() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + @FailureExpected(jiraKey = "HHH-4642") + public void testReadOnlyModifiedToModifiableIsUpdated(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); s.setReadOnly( dp, true ); - checkReadOnly( s, dp,true ); + checkReadOnly( s, dp, true ); + dp.setDescription( "changed" ); + assertTrue( Hibernate.isInitialized( dp ) ); + assertEquals( "changed", dp.getDescription() ); + s.setReadOnly( dp, false ); + checkReadOnly( s, dp, false ); + assertEquals( "changed", dp.getDescription() ); + s.flush(); + s.getTransaction().commit(); + s.close(); + + s = openSession( scope ); + s.beginTransaction(); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); + assertEquals( dpOrig.getId(), dp.getId() ); + assertEquals( dpOrig.getDescription(), dp.getDescription() ); + assertEquals( dpOrig.getX(), dp.getX() ); + assertEquals( dpOrig.getY(), dp.getY() ); + try { + assertEquals( "changed", dp.getDescription() ); + // should fail due to HHH-4642 + } + finally { + s.getTransaction().rollback(); + s.close(); + s = openSession( scope ); + s.beginTransaction(); + s.delete( dp ); + s.getTransaction().commit(); + s.close(); + } + } + + @Test + public void testReadOnlyChangedEvictedUpdate(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); + + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); + s.beginTransaction(); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + assertTrue( dp instanceof HibernateProxy ); + assertFalse( Hibernate.isInitialized( dp ) ); + checkReadOnly( s, dp, false ); + s.setReadOnly( dp, true ); + checkReadOnly( s, dp, true ); dp.setDescription( "changed" ); assertTrue( Hibernate.isInitialized( dp ) ); assertEquals( "changed", dp.getDescription() ); @@ -686,9 +696,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -696,23 +706,23 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.delete( dp ); s.getTransaction().commit(); s.close(); - } + } @Test - public void testReadOnlyToModifiableInitWhenModifiedIsUpdated() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyToModifiableInitWhenModifiedIsUpdated(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); s.setReadOnly( dp, true ); - checkReadOnly( s, dp,true ); + checkReadOnly( s, dp, true ); s.setReadOnly( dp, false ); checkReadOnly( s, dp, false ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); dp.setDescription( "changed" ); assertTrue( Hibernate.isInitialized( dp ) ); assertEquals( "changed", dp.getDescription() ); @@ -720,9 +730,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -733,21 +743,21 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyInitToModifiableModifiedIsUpdated() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyInitToModifiableModifiedIsUpdated(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); s.setReadOnly( dp, true ); - checkReadOnly( s, dp,true ); - assertFalse( Hibernate.isInitialized( dp )); + checkReadOnly( s, dp, true ); + assertFalse( Hibernate.isInitialized( dp ) ); Hibernate.initialize( dp ); - assertTrue( Hibernate.isInitialized( dp )); - checkReadOnly( s, dp,true ); + assertTrue( Hibernate.isInitialized( dp ) ); + checkReadOnly( s, dp, true ); s.setReadOnly( dp, false ); checkReadOnly( s, dp, false ); dp.setDescription( "changed" ); @@ -757,9 +767,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -770,30 +780,30 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyModifiedUpdate() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyModifiedUpdate(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); s.setReadOnly( dp, true ); - checkReadOnly( s, dp,true ); - assertFalse( Hibernate.isInitialized( dp )); + checkReadOnly( s, dp, true ); + assertFalse( Hibernate.isInitialized( dp ) ); dp.setDescription( "changed" ); assertTrue( Hibernate.isInitialized( dp ) ); assertEquals( "changed", dp.getDescription() ); - checkReadOnly( s, dp,true ); + checkReadOnly( s, dp, true ); s.update( dp ); s.flush(); s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -804,48 +814,48 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyDelete() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyDelete(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); s.setReadOnly( dp, true ); - checkReadOnly( s, dp,true ); - assertFalse( Hibernate.isInitialized( dp )); + checkReadOnly( s, dp, true ); + assertFalse( Hibernate.isInitialized( dp ) ); s.delete( dp ); s.flush(); s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertNull( dp ); s.getTransaction().commit(); s.close(); } @Test - public void testReadOnlyRefresh() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyRefresh(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); - dp = ( DataPoint ) s.load( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.load( DataPoint.class, dp.getId() ); s.setReadOnly( dp, true ); assertFalse( Hibernate.isInitialized( dp ) ); s.refresh( dp ); @@ -855,18 +865,18 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { dp.setDescription( "changed" ); assertEquals( "changed", dp.getDescription() ); assertTrue( s.isReadOnly( dp ) ); - assertTrue( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertTrue( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); s.refresh( dp ); assertEquals( "original", dp.getDescription() ); dp.setDescription( "changed" ); assertEquals( "changed", dp.getDescription() ); assertTrue( s.isReadOnly( dp ) ); - assertTrue( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertTrue( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); t.commit(); s.clear(); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertEquals( "original", dp.getDescription() ); s.delete( dp ); t.commit(); @@ -874,37 +884,37 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyRefreshDeleted() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyRefreshDeleted(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); - HibernateProxy dpProxy = ( HibernateProxy ) s.load( DataPoint.class, dp.getId() ); + HibernateProxy dpProxy = (HibernateProxy) s.load( DataPoint.class, dp.getId() ); assertFalse( Hibernate.isInitialized( dpProxy ) ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); s.delete( dp ); s.flush(); try { s.refresh( dp ); fail( "should have thrown UnresolvableObjectException" ); } - catch ( UnresolvableObjectException ex ) { + catch (UnresolvableObjectException ex) { // expected } finally { @@ -912,16 +922,16 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.close(); } - s = openSession(); + s = openSession( scope ); t = s.beginTransaction(); - s.setCacheMode(CacheMode.IGNORE); - DataPoint dpProxyInit = ( DataPoint ) s.load( DataPoint.class, dp.getId() ); + s.setCacheMode( CacheMode.IGNORE ); + DataPoint dpProxyInit = (DataPoint) s.load( DataPoint.class, dp.getId() ); assertEquals( "original", dp.getDescription() ); s.delete( dpProxyInit ); t.commit(); s.close(); - s = openSession(); + s = openSession( scope ); t = s.beginTransaction(); assertTrue( dpProxyInit instanceof HibernateProxy ); assertTrue( Hibernate.isInitialized( dpProxyInit ) ); @@ -929,7 +939,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.refresh( dpProxyInit ); fail( "should have thrown UnresolvableObjectException" ); } - catch ( UnresolvableObjectException ex ) { + catch (UnresolvableObjectException ex) { // expected } finally { @@ -937,7 +947,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.close(); } - s = openSession(); + s = openSession( scope ); t = s.beginTransaction(); assertTrue( dpProxy instanceof HibernateProxy ); try { @@ -946,7 +956,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { Hibernate.initialize( dpProxy ); fail( "should have thrown UnresolvableObjectException" ); } - catch ( UnresolvableObjectException ex ) { + catch (UnresolvableObjectException ex) { // expected } finally { @@ -956,22 +966,22 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyRefreshDetached() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyRefreshDetached(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); - dp = ( DataPoint ) s.load( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.load( DataPoint.class, dp.getId() ); assertFalse( Hibernate.isInitialized( dp ) ); assertFalse( s.isReadOnly( dp ) ); s.setReadOnly( dp, true ); @@ -992,7 +1002,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.clear(); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertEquals( "original", dp.getDescription() ); s.delete( dp ); t.commit(); @@ -1000,16 +1010,16 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyProxyMergeDetachedProxyWithChange() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyProxyMergeDetachedProxyWithChange(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); Hibernate.initialize( dp ); assertTrue( Hibernate.isInitialized( dp ) ); s.getTransaction().commit(); @@ -1018,16 +1028,16 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { // modify detached proxy dp.setDescription( "changed" ); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dpLoaded = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dpLoaded = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dpLoaded instanceof HibernateProxy ); checkReadOnly( s, dpLoaded, false ); s.setReadOnly( dpLoaded, true ); - checkReadOnly( s, dpLoaded,true ); + checkReadOnly( s, dpLoaded, true ); assertFalse( Hibernate.isInitialized( dpLoaded ) ); - DataPoint dpMerged = ( DataPoint ) s.merge( dp ); + DataPoint dpMerged = (DataPoint) s.merge( dp ); assertSame( dpLoaded, dpMerged ); assertTrue( Hibernate.isInitialized( dpLoaded ) ); assertEquals( "changed", dpLoaded.getDescription() ); @@ -1036,9 +1046,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1049,16 +1059,16 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyProxyInitMergeDetachedProxyWithChange() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyProxyInitMergeDetachedProxyWithChange(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); Hibernate.initialize( dp ); assertTrue( Hibernate.isInitialized( dp ) ); s.getTransaction().commit(); @@ -1067,18 +1077,18 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { // modify detached proxy dp.setDescription( "changed" ); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dpLoaded = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dpLoaded = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dpLoaded instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dpLoaded ) ); Hibernate.initialize( dpLoaded ); assertTrue( Hibernate.isInitialized( dpLoaded ) ); checkReadOnly( s, dpLoaded, false ); s.setReadOnly( dpLoaded, true ); - checkReadOnly( s, dpLoaded,true ); - DataPoint dpMerged = ( DataPoint ) s.merge( dp ); + checkReadOnly( s, dpLoaded, true ); + DataPoint dpMerged = (DataPoint) s.merge( dp ); assertSame( dpLoaded, dpMerged ); assertEquals( "changed", dpLoaded.getDescription() ); checkReadOnly( s, dpLoaded, true ); @@ -1086,9 +1096,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1099,35 +1109,35 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyProxyMergeDetachedEntityWithChange() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyProxyMergeDetachedEntityWithChange(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); Hibernate.initialize( dp ); assertTrue( Hibernate.isInitialized( dp ) ); s.getTransaction().commit(); s.close(); // modify detached proxy target - DataPoint dpEntity = ( DataPoint ) ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation(); + DataPoint dpEntity = (DataPoint) ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation(); dpEntity.setDescription( "changed" ); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dpLoaded = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dpLoaded = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dpLoaded instanceof HibernateProxy ); checkReadOnly( s, dpLoaded, false ); s.setReadOnly( dpLoaded, true ); - checkReadOnly( s, dpLoaded,true ); + checkReadOnly( s, dpLoaded, true ); assertFalse( Hibernate.isInitialized( dpLoaded ) ); - DataPoint dpMerged = ( DataPoint ) s.merge( dpEntity ); + DataPoint dpMerged = (DataPoint) s.merge( dpEntity ); assertSame( dpLoaded, dpMerged ); assertTrue( Hibernate.isInitialized( dpLoaded ) ); assertEquals( "changed", dpLoaded.getDescription() ); @@ -1136,9 +1146,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1149,37 +1159,37 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyProxyInitMergeDetachedEntityWithChange() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyProxyInitMergeDetachedEntityWithChange(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); Hibernate.initialize( dp ); assertTrue( Hibernate.isInitialized( dp ) ); s.getTransaction().commit(); s.close(); // modify detached proxy target - DataPoint dpEntity = ( DataPoint ) ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation(); + DataPoint dpEntity = (DataPoint) ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation(); dpEntity.setDescription( "changed" ); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dpLoaded = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dpLoaded = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dpLoaded instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dpLoaded ) ); Hibernate.initialize( dpLoaded ); assertTrue( Hibernate.isInitialized( dpLoaded ) ); checkReadOnly( s, dpLoaded, false ); s.setReadOnly( dpLoaded, true ); - checkReadOnly( s, dpLoaded,true ); - DataPoint dpMerged = ( DataPoint ) s.merge( dpEntity ); + checkReadOnly( s, dpLoaded, true ); + DataPoint dpMerged = (DataPoint) s.merge( dpEntity ); assertSame( dpLoaded, dpMerged ); assertEquals( "changed", dpLoaded.getDescription() ); checkReadOnly( s, dpLoaded, true ); @@ -1187,9 +1197,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1200,16 +1210,16 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyEntityMergeDetachedProxyWithChange() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testReadOnlyEntityMergeDetachedProxyWithChange(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); checkReadOnly( s, dp, false ); - assertFalse( Hibernate.isInitialized( dp )); + assertFalse( Hibernate.isInitialized( dp ) ); Hibernate.initialize( dp ); assertTrue( Hibernate.isInitialized( dp ) ); s.getTransaction().commit(); @@ -1218,15 +1228,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { // modify detached proxy dp.setDescription( "changed" ); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dpEntity = ( DataPoint ) s.get( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dpEntity = (DataPoint) s.get( DataPoint.class, new Long( dpOrig.getId() ) ); assertFalse( dpEntity instanceof HibernateProxy ); assertFalse( s.isReadOnly( dpEntity ) ); s.setReadOnly( dpEntity, true ); assertTrue( s.isReadOnly( dpEntity ) ); - DataPoint dpMerged = ( DataPoint ) s.merge( dp ); + DataPoint dpMerged = (DataPoint) s.merge( dp ); assertSame( dpEntity, dpMerged ); assertEquals( "changed", dpEntity.getDescription() ); assertTrue( s.isReadOnly( dpEntity ) ); @@ -1234,9 +1244,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1247,14 +1257,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testSetReadOnlyInTwoTransactionsSameSession() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testSetReadOnlyInTwoTransactionsSameSession(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1278,9 +1288,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( dpOrig.getDescription(), dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1291,14 +1301,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testSetReadOnlyBetweenTwoTransactionsSameSession() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testSetReadOnlyBetweenTwoTransactionsSameSession(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1322,9 +1332,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1335,14 +1345,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testSetModifiableBetweenTwoTransactionsSameSession() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testSetModifiableBetweenTwoTransactionsSameSession(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = s.load( DataPoint.class, dpOrig.getId() ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1372,9 +1382,9 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() ); + dp = s.get( DataPoint.class, dpOrig.getId() ); assertEquals( dpOrig.getId(), dp.getId() ); assertEquals( "changed again", dp.getDescription() ); assertEquals( dpOrig.getX(), dp.getX() ); @@ -1385,14 +1395,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testIsReadOnlyAfterSessionClosed() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testIsReadOnlyAfterSessionClosed(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = s.load( DataPoint.class, dpOrig.getId() ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1400,15 +1410,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.close(); try { - s.isReadOnly( dp ); + s.isReadOnly( dp ); fail( "should have failed because session was closed" ); } - catch ( IllegalStateException ex) { + catch (IllegalStateException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { - s = openSession(); + s = openSession( scope ); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); @@ -1417,14 +1427,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testIsReadOnlyAfterSessionClosedViaLazyInitializer() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testIsReadOnlyAfterSessionClosedViaLazyInitializer(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = s.load( DataPoint.class, dpOrig.getId() ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1432,17 +1442,17 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { assertTrue( s.contains( dp ) ); s.close(); - assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); + assertNull( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getSession() ); try { - ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnly(); + ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnly(); fail( "should have failed because session was detached" ); } - catch ( TransientObjectException ex) { + catch (TransientObjectException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { - s = openSession(); + s = openSession( scope ); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); @@ -1451,29 +1461,29 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testDetachedIsReadOnlyAfterEvictViaSession() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testDetachedIsReadOnlyAfterEvictViaSession(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); assertTrue( s.contains( dp ) ); s.evict( dp ); assertFalse( s.contains( dp ) ); - assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); + assertNull( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getSession() ); try { - s.isReadOnly( dp ); + s.isReadOnly( dp ); fail( "should have failed because proxy was detached" ); } - catch ( TransientObjectException ex) { + catch (TransientObjectException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); @@ -1483,27 +1493,27 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testDetachedIsReadOnlyAfterEvictViaLazyInitializer() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testDetachedIsReadOnlyAfterEvictViaLazyInitializer(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); s.evict( dp ); assertFalse( s.contains( dp ) ); - assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); + assertNull( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getSession() ); try { - ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnly(); + ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnly(); fail( "should have failed because proxy was detached" ); } - catch ( TransientObjectException ex) { + catch (TransientObjectException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); @@ -1513,14 +1523,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testSetReadOnlyAfterSessionClosed() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testSetReadOnlyAfterSessionClosed(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1528,15 +1538,15 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { s.close(); try { - s.setReadOnly( dp, true ); + s.setReadOnly( dp, true ); fail( "should have failed because session was closed" ); } - catch ( IllegalStateException ex) { + catch (IllegalStateException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { - s = openSession(); + s = openSession( scope ); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); @@ -1545,14 +1555,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testSetReadOnlyAfterSessionClosedViaLazyInitializer() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testSetReadOnlyAfterSessionClosedViaLazyInitializer(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1560,17 +1570,17 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { assertTrue( s.contains( dp ) ); s.close(); - assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); + assertNull( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getSession() ); try { - ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().setReadOnly( true ); + ( (HibernateProxy) dp ).getHibernateLazyInitializer().setReadOnly( true ); fail( "should have failed because session was detached" ); } - catch ( TransientObjectException ex) { + catch (TransientObjectException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { - s = openSession(); + s = openSession( scope ); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); @@ -1579,14 +1589,14 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testSetClosedSessionInLazyInitializer() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testSetClosedSessionInLazyInitializer(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); @@ -1594,18 +1604,18 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { assertTrue( s.contains( dp ) ); s.close(); - assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); + assertNull( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getSession() ); assertTrue( ( (SessionImplementor) s ).isClosed() ); try { - ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().setSession( ( SessionImplementor ) s ); + ( (HibernateProxy) dp ).getHibernateLazyInitializer().setSession( (SessionImplementor) s ); fail( "should have failed because session was closed" ); } - catch ( IllegalStateException ex) { + catch (IllegalStateException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { - s = openSession(); + s = openSession( scope ); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); @@ -1614,29 +1624,29 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testDetachedSetReadOnlyAfterEvictViaSession() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testDetachedSetReadOnlyAfterEvictViaSession(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); assertTrue( s.contains( dp ) ); s.evict( dp ); assertFalse( s.contains( dp ) ); - assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); + assertNull( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getSession() ); try { - s.setReadOnly( dp, true ); + s.setReadOnly( dp, true ); fail( "should have failed because proxy was detached" ); } - catch ( TransientObjectException ex) { + catch (TransientObjectException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); @@ -1646,27 +1656,27 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } @Test - public void testDetachedSetReadOnlyAfterEvictViaLazyInitializer() { - DataPoint dpOrig = createDataPoint( CacheMode.IGNORE ); + public void testDetachedSetReadOnlyAfterEvictViaLazyInitializer(SessionFactoryScope scope) { + DataPoint dpOrig = createDataPoint( CacheMode.IGNORE, scope ); - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); - DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); + DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long( dpOrig.getId() ) ); assertTrue( dp instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( dp ) ); checkReadOnly( s, dp, false ); s.evict( dp ); assertFalse( s.contains( dp ) ); - assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); + assertNull( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getSession() ); try { - ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().setReadOnly( true ); + ( (HibernateProxy) dp ).getHibernateLazyInitializer().setReadOnly( true ); fail( "should have failed because proxy was detached" ); } - catch ( TransientObjectException ex) { + catch (TransientObjectException ex) { // expected - assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); + assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); @@ -1675,13 +1685,17 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { } } - private DataPoint createDataPoint(CacheMode cacheMode) { - Session s = openSession(); + private Session openSession(SessionFactoryScope scope) { + return scope.getSessionFactory().openSession(); + } + + private DataPoint createDataPoint(CacheMode cacheMode, SessionFactoryScope scope) { + Session s = openSession( scope ); s.setCacheMode( cacheMode ); s.beginTransaction(); DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal( 0.1d ).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); dp.setDescription( "original" ); s.save( dp ); s.getTransaction().commit(); @@ -1691,11 +1705,11 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest { private void checkReadOnly(Session s, Object proxy, boolean expectedReadOnly) { assertTrue( proxy instanceof HibernateProxy ); - LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer(); + LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer(); assertSame( s, li.getSession() ); assertEquals( expectedReadOnly, s.isReadOnly( proxy ) ); assertEquals( expectedReadOnly, li.isReadOnly() ); - assertEquals( Hibernate.isInitialized( proxy ), ! li.isUninitialized() ); + assertEquals( Hibernate.isInitialized( proxy ), !li.isUninitialized() ); if ( Hibernate.isInitialized( proxy ) ) { assertEquals( expectedReadOnly, s.isReadOnly( li.getImplementation() ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionLazyNonLazyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlySessionLazyNonLazyTest.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionLazyNonLazyTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlySessionLazyNonLazyTest.java index 9a718a2e6a..193a67212b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionLazyNonLazyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlySessionLazyNonLazyTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.readonly; +package org.hibernate.orm.test.readonly; import java.math.BigDecimal; import java.util.Arrays; @@ -18,14 +18,21 @@ import org.hibernate.Transaction; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.proxy.HibernateProxy; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.test.readonly.AbstractReadOnlyTest; +import org.hibernate.test.readonly.Container; +import org.hibernate.test.readonly.DataPoint; +import org.hibernate.test.readonly.Info; +import org.hibernate.test.readonly.Owner; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Model: @@ -48,15 +55,14 @@ import static org.junit.Assert.assertTrue; * * @author Gail Badner */ +@DomainModel( + xmlMappings = { "org/hibernate/test/readonly/DataPoint.hbm.xml" } +) public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { - @Override - public String[] getMappings() { - return new String[] { "readonly/DataPoint.hbm.xml" }; - } @Test @SuppressWarnings( {"unchecked"}) - public void testExistingModifiableAfterSetSessionReadOnly() { + public void testExistingModifiableAfterSetSessionReadOnly(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -74,7 +80,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -97,7 +103,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { assertSame( cOrig, c ); checkContainer( cOrig, expectedInitializedObjects, expectedReadOnlyObjects, s ); s.evict( cOrig ); - c = ( Container ) s.get( Container.class, cOrig.getId() ); + c = s.get( Container.class, cOrig.getId() ); assertNotSame( cOrig, c ); expectedInitializedObjects = new HashSet( Arrays.asList( @@ -140,7 +146,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -153,7 +159,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @Test @SuppressWarnings( {"unchecked"}) - public void testExistingReadOnlyAfterSetSessionModifiable() { + public void testExistingReadOnlyAfterSetSessionModifiable(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( @@ -171,7 +177,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ) ); Set expectedReadOnlyObjects = new HashSet(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -182,7 +188,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.setDefaultReadOnly( true ); Container c = ( Container ) s.get( Container.class, cOrig.getId() ); @@ -230,7 +236,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -243,7 +249,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @Test @SuppressWarnings( {"unchecked"}) - public void testExistingReadOnlyAfterSetSessionModifiableExisting() { + public void testExistingReadOnlyAfterSetSessionModifiableExisting(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -261,7 +267,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); DataPoint lazyDataPointOrig = ( DataPoint ) cOrig.getLazyDataPoints().iterator().next(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -272,7 +278,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.setDefaultReadOnly( true ); Container c = ( Container ) s.get( Container.class, cOrig.getId() ); @@ -321,7 +327,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -334,7 +340,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testExistingReadOnlyAfterSetSessionModifiableExistingEntityReadOnly() { + public void testExistingReadOnlyAfterSetSessionModifiableExistingEntityReadOnly(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -352,7 +358,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); DataPoint lazyDataPointOrig = ( DataPoint ) cOrig.getLazyDataPoints().iterator().next(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -363,7 +369,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.setDefaultReadOnly( true ); Container c = ( Container ) s.get( Container.class, cOrig.getId() ); @@ -415,7 +421,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -428,7 +434,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testExistingReadOnlyAfterSetSessionModifiableProxyExisting() { + public void testExistingReadOnlyAfterSetSessionModifiableProxyExisting(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -446,7 +452,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); DataPoint lazyDataPointOrig = ( DataPoint ) cOrig.getLazyDataPoints().iterator().next(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -457,7 +463,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.setDefaultReadOnly( true ); Container c = ( Container ) s.get( Container.class, cOrig.getId() ); @@ -506,7 +512,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -519,7 +525,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testExistingReadOnlyAfterSetSessionModifiableExistingProxyReadOnly() { + public void testExistingReadOnlyAfterSetSessionModifiableExistingProxyReadOnly(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -537,7 +543,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); DataPoint lazyDataPointOrig = ( DataPoint ) cOrig.getLazyDataPoints().iterator().next(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -548,7 +554,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.setDefaultReadOnly( true ); Container c = ( Container ) s.get( Container.class, cOrig.getId() ); @@ -600,7 +606,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -612,7 +618,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testDefaultModifiableWithReadOnlyQueryForEntity() { + public void testDefaultModifiableWithReadOnlyQueryForEntity(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -630,7 +636,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -641,7 +647,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); assertFalse( s.isDefaultReadOnly() ); Container c = ( Container ) s.createQuery( "from Container where id=" + cOrig.getId() ) @@ -687,7 +693,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -699,7 +705,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testDefaultReadOnlyWithModifiableQueryForEntity() { + public void testDefaultReadOnlyWithModifiableQueryForEntity(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -717,7 +723,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -728,7 +734,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.setDefaultReadOnly( true ); assertTrue( s.isDefaultReadOnly() ); @@ -762,7 +768,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -774,7 +780,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testDefaultReadOnlyWithQueryForEntity() { + public void testDefaultReadOnlyWithQueryForEntity(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -792,7 +798,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -803,7 +809,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.setDefaultReadOnly( true ); assertTrue( s.isDefaultReadOnly() ); @@ -850,7 +856,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -862,7 +868,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testDefaultModifiableWithQueryForEntity() { + public void testDefaultModifiableWithQueryForEntity(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -880,7 +886,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -891,7 +897,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); assertFalse( s.isDefaultReadOnly() ); Container c = ( Container ) s.createQuery( "from Container where id=" + cOrig.getId() ) @@ -924,7 +930,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { checkContainer( c, expectedInitializedObjects, expectedReadOnlyObjects, s ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -936,7 +942,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { @SuppressWarnings( {"unchecked"}) @Test - public void testDefaultModifiableWithReadOnlyQueryForCollectionEntities() { + public void testDefaultModifiableWithReadOnlyQueryForCollectionEntities(SessionFactoryScope scope) { Container cOrig = createContainer(); Set expectedInitializedObjects = new HashSet( Arrays.asList( @@ -954,7 +960,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { ); Set expectedReadOnlyObjects = new HashSet(); - Session s = openSession(); + Session s = openSession(scope); assertFalse( s.isDefaultReadOnly() ); Transaction t = s.beginTransaction(); s.save( cOrig ); @@ -965,7 +971,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); assertFalse( s.isDefaultReadOnly() ); DataPoint dp = ( DataPoint ) s.createQuery( "select c.lazyDataPoints from Container c join c.lazyDataPoints where c.id=" + cOrig.getId() ) @@ -973,7 +979,7 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { assertTrue( s.isReadOnly( dp ) ); t.commit(); s.close(); - s = openSession(); + s = openSession(scope); t = s.beginTransaction(); s.createQuery("delete from DataPoint").executeUpdate(); s.createQuery("delete from Container").executeUpdate(); @@ -1037,4 +1043,8 @@ public class ReadOnlySessionLazyNonLazyTest extends AbstractReadOnlyTest { } } + private Session openSession(SessionFactoryScope scope) { + return scope.getSessionFactory().openSession(); + } + } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyTest.java new file mode 100644 index 0000000000..0118ea0c41 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyTest.java @@ -0,0 +1,658 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.readonly; + +import java.math.BigDecimal; +import java.util.List; + +import org.hibernate.Hibernate; +import org.hibernate.ScrollMode; +import org.hibernate.ScrollableResults; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.test.readonly.AbstractReadOnlyTest; +import org.hibernate.test.readonly.DataPoint; +import org.hibernate.test.readonly.TextHolder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Gavin King + * @author Gail Badner + */ +@DomainModel( + xmlMappings = { + "org/hibernate/test/readonly/DataPoint.hbm.xml", + "org/hibernate/test/readonly/TextHolder.hbm.xml" + } +) +public class ReadOnlyTest extends AbstractReadOnlyTest { + + @Test + public void testReadOnlyOnProxies(SessionFactoryScope scope) { + clearCounts( scope ); + + long dpId = scope.fromTransaction( + session -> { + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( + 19, + BigDecimal.ROUND_DOWN + ) ); + dp.setDescription( "original" ); + session.save( dp ); + return dp.getId(); + } + ); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + DataPoint dp = session.load( DataPoint.class, new Long( dpId ) ); + assertFalse( Hibernate.isInitialized( dp ), "was initialized" ); + session.setReadOnly( dp, true ); + assertFalse( Hibernate.isInitialized( dp ), "was initialized during setReadOnly" ); + dp.setDescription( "changed" ); + assertTrue( Hibernate.isInitialized( dp ), "was not initialized during mod" ); + assertEquals( "changed", dp.getDescription(), "desc not changed in memory" ); + session.flush(); + } + ); + + assertUpdateCount( 0, scope ); + + scope.inTransaction( + session -> { + List list = session.createQuery( "from DataPoint where description = 'changed'" ).list(); + assertEquals( 0, list.size(), "change written to database" ); + assertEquals( 1, session.createQuery( "delete from DataPoint" ).executeUpdate() ); + + } + ); + + assertUpdateCount( 0, scope ); + //deletes from Query.executeUpdate() are not tracked + //assertDeleteCount( 1 ); + } + + @Test + public void testReadOnlyMode(SessionFactoryScope scope) { + clearCounts( scope ); + + scope.inTransaction( + session -> { + for ( int i = 0; i < 100; i++ ) { + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( + 19, + BigDecimal.ROUND_DOWN + ) ); + session.save( dp ); + } + } + ); + + assertInsertCount( 100, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + scope.inSession( + session -> { + try { + session.getTransaction().begin(); + int i = 0; + ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" ) + .setReadOnly( true ) + .scroll( ScrollMode.FORWARD_ONLY ); + while ( sr.next() ) { + DataPoint dp = (DataPoint) sr.get(); + if ( ++i == 50 ) { + session.setReadOnly( dp, false ); + } + dp.setDescription( "done!" ); + } + session.getTransaction().commit(); + + assertUpdateCount( 1, scope ); + clearCounts( scope ); + + session.clear(); + session.getTransaction().begin(); + + List single = session.createQuery( "from DataPoint where description='done!'" ).list(); + assertEquals( single.size(), 1 ); + assertEquals( 100, session.createQuery( "delete from DataPoint" ).executeUpdate() ); + session.getTransaction().commit(); + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + + } + ); + + assertUpdateCount( 0, scope ); + //deletes from Query.executeUpdate() are not tracked + //assertDeleteCount( 100 ); + } + + @Test + public void testReadOnlyModeAutoFlushOnQuery(SessionFactoryScope scope) { + clearCounts( scope ); + + scope.inTransaction( + session -> { + DataPoint dpFirst = null; + for ( int i = 0; i < 100; i++ ) { + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( + 19, + BigDecimal.ROUND_DOWN + ) ); + session.save( dp ); + } + + assertInsertCount( 0, scope ); + assertUpdateCount( 0, scope ); + + ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" ) + .setReadOnly( true ) + .scroll( ScrollMode.FORWARD_ONLY ); + + assertInsertCount( 100, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + while ( sr.next() ) { + DataPoint dp = (DataPoint) sr.get(); + assertFalse( session.isReadOnly( dp ) ); + session.delete( dp ); + } + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 100, scope ); + } + + @Test + public void testSaveReadOnlyModifyInSaveTransaction(SessionFactoryScope scope) { + clearCounts( scope ); + + DataPoint d = new DataPoint(); + scope.inTransaction( + session -> { + d.setDescription( "original" ); + d.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + d.setY( new BigDecimal( Math.cos( d.getX().doubleValue() ) ).setScale( + 19, + BigDecimal.ROUND_DOWN + ) ); + session.save( d ); + session.setReadOnly( d, true ); + d.setDescription( "different" ); + } + ); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + scope.inSession( + session -> { + try { + session.beginTransaction(); + DataPoint dp = session.get( DataPoint.class, d.getId() ); + session.setReadOnly( dp, true ); + assertEquals( "original", dp.getDescription() ); + dp.setDescription( "changed" ); + assertEquals( "changed", dp.getDescription() ); + session.refresh( dp ); + assertEquals( "original", dp.getDescription() ); + dp.setDescription( "changed" ); + assertEquals( "changed", dp.getDescription() ); + session.getTransaction().commit(); + + assertInsertCount( 0, scope ); + assertUpdateCount( 0, scope ); + + session.clear(); + session.beginTransaction(); + dp = session.get( DataPoint.class, dp.getId() ); + assertEquals( "original", dp.getDescription() ); + session.delete( dp ); + session.getTransaction().commit(); + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + clearCounts( scope ); + } + + @Test + public void testReadOnlyRefresh(SessionFactoryScope scope) { + clearCounts( scope ); + + DataPoint d = new DataPoint(); + scope.inTransaction( + session -> { + d.setDescription( "original" ); + d.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + d.setY( new BigDecimal( Math.cos( d.getX().doubleValue() ) ).setScale( + 19, + BigDecimal.ROUND_DOWN + ) ); + session.save( d ); + } + ); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + scope.inSession( + session -> { + try { + session.beginTransaction(); + + DataPoint dp = session.get( DataPoint.class, d.getId() ); + session.setReadOnly( dp, true ); + assertEquals( "original", dp.getDescription() ); + dp.setDescription( "changed" ); + assertEquals( "changed", dp.getDescription() ); + session.refresh( dp ); + assertEquals( "original", dp.getDescription() ); + dp.setDescription( "changed" ); + assertEquals( "changed", dp.getDescription() ); + session.getTransaction().commit(); + + assertInsertCount( 0, scope ); + assertUpdateCount( 0, scope ); + + session.clear(); + session.beginTransaction(); + dp = session.get( DataPoint.class, dp.getId() ); + assertEquals( "original", dp.getDescription() ); + session.delete( dp ); + session.getTransaction().commit(); + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + clearCounts( scope ); + } + + @Test + public void testReadOnlyRefreshDetached(SessionFactoryScope scope) { + clearCounts( scope ); + + Session s = openSession( scope ); + Transaction t = s.beginTransaction(); + DataPoint dp = new DataPoint(); + dp.setDescription( "original" ); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); + t.commit(); + s.close(); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + dp.setDescription( "changed" ); + assertEquals( "changed", dp.getDescription() ); + s.refresh( dp ); + assertEquals( "original", dp.getDescription() ); + assertFalse( s.isReadOnly( dp ) ); + s.setReadOnly( dp, true ); + dp.setDescription( "changed" ); + assertEquals( "changed", dp.getDescription() ); + s.evict( dp ); + s.refresh( dp ); + assertEquals( "original", dp.getDescription() ); + assertFalse( s.isReadOnly( dp ) ); + t.commit(); + + assertInsertCount( 0, scope ); + assertUpdateCount( 0, scope ); + + s.clear(); + t = s.beginTransaction(); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); + assertEquals( "original", dp.getDescription() ); + s.delete( dp ); + t.commit(); + s.close(); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + } + + @Test + public void testReadOnlyDelete(SessionFactoryScope scope) { + clearCounts( scope ); + + Session s = openSession( scope ); + Transaction t = s.beginTransaction(); + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); + t.commit(); + s.close(); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); + s.setReadOnly( dp, true ); + s.delete( dp ); + t.commit(); + s.close(); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); + assertTrue( list.isEmpty() ); + t.commit(); + s.close(); + + } + + @Test + public void testReadOnlyGetModifyAndDelete(SessionFactoryScope scope) { + clearCounts( scope ); + + Session s = openSession( scope ); + Transaction t = s.beginTransaction(); + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); + t.commit(); + s.close(); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); + s.setReadOnly( dp, true ); + dp.setDescription( "a DataPoint" ); + s.delete( dp ); + t.commit(); + s.close(); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + clearCounts( scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); + assertTrue( list.isEmpty() ); + t.commit(); + s.close(); + } + + @Test + public void testReadOnlyModeWithExistingModifiableEntity(SessionFactoryScope scope) { + clearCounts( scope ); + + Session s = openSession( scope ); + Transaction t = s.beginTransaction(); + DataPoint dp = null; + for ( int i = 0; i < 100; i++ ) { + dp = new DataPoint(); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); + } + t.commit(); + s.close(); + + assertInsertCount( 100, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); + assertFalse( s.isReadOnly( dpLast ) ); + int i = 0; + ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) + .setReadOnly( true ) + .scroll( ScrollMode.FORWARD_ONLY ); + int nExpectedChanges = 0; + while ( sr.next() ) { + dp = (DataPoint) sr.get(); + if ( dp.getId() == dpLast.getId() ) { + //dpLast existed in the session before executing the read-only query + assertFalse( s.isReadOnly( dp ) ); + } + else { + assertTrue( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + nExpectedChanges = ( dp == dpLast ? 1 : 2 ); + } + dp.setDescription( "done!" ); + } + t.commit(); + s.clear(); + + assertInsertCount( 0, scope ); + assertUpdateCount( nExpectedChanges, scope ); + clearCounts( scope ); + + t = s.beginTransaction(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); + assertEquals( list.size(), nExpectedChanges ); + assertEquals( 100, s.createQuery( "delete from DataPoint" ).executeUpdate() ); + t.commit(); + s.close(); + + assertUpdateCount( 0, scope ); + } + + @Test + public void testModifiableModeWithExistingReadOnlyEntity(SessionFactoryScope scope) { + clearCounts( scope ); + + Session s = openSession( scope ); + Transaction t = s.beginTransaction(); + DataPoint dp = null; + for ( int i = 0; i < 100; i++ ) { + dp = new DataPoint(); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); + } + t.commit(); + s.close(); + + assertInsertCount( 100, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); + assertFalse( s.isReadOnly( dpLast ) ); + s.setReadOnly( dpLast, true ); + assertTrue( s.isReadOnly( dpLast ) ); + dpLast.setDescription( "oy" ); + int i = 0; + + assertUpdateCount( 0, scope ); + + ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) + .setReadOnly( false ) + .scroll( ScrollMode.FORWARD_ONLY ); + int nExpectedChanges = 0; + while ( sr.next() ) { + dp = (DataPoint) sr.get(); + if ( dp.getId() == dpLast.getId() ) { + //dpLast existed in the session before executing the read-only query + assertTrue( s.isReadOnly( dp ) ); + } + else { + assertFalse( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); + nExpectedChanges = ( dp == dpLast ? 99 : 98 ); + } + dp.setDescription( "done!" ); + } + t.commit(); + s.clear(); + + assertUpdateCount( nExpectedChanges, scope ); + clearCounts( scope ); + + t = s.beginTransaction(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); + assertEquals( list.size(), nExpectedChanges ); + assertEquals( 100, s.createQuery( "delete from DataPoint" ).executeUpdate() ); + t.commit(); + s.close(); + + assertUpdateCount( 0, scope ); + } + + @Test + public void testReadOnlyOnTextType(SessionFactoryScope scope) { + final String origText = "some huge text string"; + final String newText = "some even bigger text string"; + + clearCounts( scope ); + + Session s = openSession( scope ); + s.beginTransaction(); + TextHolder holder = new TextHolder( origText ); + s.save( holder ); + Long id = holder.getId(); + s.getTransaction().commit(); + s.close(); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + s = openSession( scope ); + s.beginTransaction(); + holder = (TextHolder) s.get( TextHolder.class, id ); + s.setReadOnly( holder, true ); + holder.setTheText( newText ); + s.flush(); + s.getTransaction().commit(); + s.close(); + + assertUpdateCount( 0, scope ); + + s = openSession( scope ); + s.beginTransaction(); + holder = (TextHolder) s.get( TextHolder.class, id ); + assertEquals( origText, holder.getTheText(), "change written to database" ); + s.delete( holder ); + s.getTransaction().commit(); + s.close(); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + } + + @Test + public void testMergeWithReadOnlyEntity(SessionFactoryScope scope) { + clearCounts( scope ); + + Session s = openSession( scope ); + Transaction t = s.beginTransaction(); + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); + t.commit(); + s.close(); + + assertInsertCount( 1, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); + + dp.setDescription( "description" ); + + s = openSession( scope ); + t = s.beginTransaction(); + DataPoint dpManaged = (DataPoint) s.get( DataPoint.class, new Long( dp.getId() ) ); + s.setReadOnly( dpManaged, true ); + DataPoint dpMerged = (DataPoint) s.merge( dp ); + assertSame( dpManaged, dpMerged ); + t.commit(); + s.close(); + + assertUpdateCount( 0, scope ); + + s = openSession( scope ); + t = s.beginTransaction(); + dpManaged = (DataPoint) s.get( DataPoint.class, new Long( dp.getId() ) ); + assertNull( dpManaged.getDescription() ); + s.delete( dpManaged ); + t.commit(); + s.close(); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + } + + private Session openSession(SessionFactoryScope scope) { + return scope.getSessionFactory().openSession(); + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyVersionedNodesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyVersionedNodesTest.java new file mode 100644 index 0000000000..2b74660c70 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyVersionedNodesTest.java @@ -0,0 +1,658 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.readonly; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.test.readonly.AbstractReadOnlyTest; +import org.hibernate.test.readonly.VersionedNode; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +/** + * @author Gail Badner + */ +@DomainModel( + xmlMappings = "org/hibernate/test/readonly/VersionedNode.hbm.xml" +) +public class ReadOnlyVersionedNodesTest extends AbstractReadOnlyTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "select v from VersionedNode v" ).list() + .forEach( node -> session.delete( node ) ); + } + ); + } + + @Test + public void testSetReadOnlyTrueAndFalse(SessionFactoryScope scope) { + VersionedNode n = createVersionNode( scope ); + + clearCounts( scope ); + + scope.inSession( + session -> { + try { + session.beginTransaction(); + + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + session.setReadOnly( node, true ); + node.setName( "node-name" ); + session.getTransaction().commit(); + + assertUpdateCount( 0, scope ); + assertInsertCount( 0, scope ); + + // the changed name is still in node + assertEquals( "node-name", node.getName() ); + + session.beginTransaction(); + node = session.get( VersionedNode.class, node.getId() ); + // the changed name is still in the session + assertEquals( "node-name", node.getName() ); + session.refresh( node ); + // after refresh, the name reverts to the original value + assertEquals( "node", node.getName() ); + node = session.get( VersionedNode.class, node.getId() ); + assertEquals( "node", node.getName() ); + session.getTransaction().commit(); + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + } + ); + + assertUpdateCount( 0, scope ); + assertInsertCount( 0, scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + assertEquals( "node", node.getName() ); + session.setReadOnly( node, true ); + node.setName( "diff-node-name" ); + session.flush(); + assertEquals( "diff-node-name", node.getName() ); + session.refresh( node ); + assertEquals( "node", node.getName() ); + session.setReadOnly( node, false ); + node.setName( "diff-node-name" ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 0, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + assertEquals( "diff-node-name", node.getName() ); + assertEquals( 1, node.getVersion() ); + session.setReadOnly( node, true ); + session.delete( node ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + } + + @Test + public void testUpdateSetReadOnlyTwice(SessionFactoryScope scope) { + VersionedNode n = createVersionNode( scope ); + + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + node.setName( "node-name" ); + session.setReadOnly( node, true ); + session.setReadOnly( node, true ); + } + ); + + assertUpdateCount( 0, scope ); + assertInsertCount( 0, scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + assertEquals( "node", node.getName() ); + assertEquals( 0, node.getVersion() ); + session.setReadOnly( node, true ); + session.delete( node ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + } + + @Test + public void testUpdateSetModifiable(SessionFactoryScope scope) { + VersionedNode n = createVersionNode( scope ); + + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + node.setName( "node-name" ); + session.setReadOnly( node, false ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 0, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + assertEquals( "node-name", node.getName() ); + assertEquals( 1, node.getVersion() ); + session.setReadOnly( node, true ); + session.delete( node ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + } + + private VersionedNode createVersionNode(SessionFactoryScope scope) { + return scope.fromTransaction( + session -> { + VersionedNode nd = new VersionedNode( "node", "node" ); + session.persist( nd ); + return nd; + } + ); + } + + private VersionedNode createVersionNode(String id, String name, SessionFactoryScope scope) { + return scope.fromTransaction( + session -> { + VersionedNode nd = new VersionedNode( id, name ); + session.persist( nd ); + return nd; + } + ); + } + + @Test + @FailureExpected(jiraKey = "unknown") + public void testUpdateSetReadOnlySetModifiable(SessionFactoryScope scope) { + VersionedNode n = createVersionNode( scope ); + + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + node.setName( "node-name" ); + session.setReadOnly( node, true ); + session.setReadOnly( node, false ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 0, scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + assertEquals( "node-name", node.getName() ); + assertEquals( 1, node.getVersion() ); + session.delete( node ); + } + ); + } + + @Test + @FailureExpected(jiraKey = "unknown") + public void testSetReadOnlyUpdateSetModifiable(SessionFactoryScope scope) { + VersionedNode n = createVersionNode( scope ); + + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + session.setReadOnly( node, true ); + node.setName( "node-name" ); + session.setReadOnly( node, false ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 0, scope ); + + scope.inTransaction( + session -> { + VersionedNode node = session.get( VersionedNode.class, n.getId() ); + assertEquals( "node-name", node.getName() ); + assertEquals( 1, node.getVersion() ); + session.delete( node ); + } + ); + } + + @Test + public void testAddNewChildToReadOnlyParent(SessionFactoryScope scope) { + VersionedNode p = createVersionNode( "parent", "parent", scope ); + + clearCounts( scope ); + + VersionedNode c = scope.fromTransaction( + session -> { + VersionedNode parentManaged = session.get( VersionedNode.class, p.getId() ); + session.setReadOnly( parentManaged, true ); + parentManaged.setName( "new parent name" ); + VersionedNode child = new VersionedNode( "child", "child" ); + parentManaged.addChild( child ); + return child; + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 1, scope ); + + scope.inTransaction( + session -> { + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + assertEquals( "parent", parent.getName() ); + assertEquals( 1, parent.getChildren().size() ); + assertEquals( 1, parent.getVersion() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertNotNull( child ); + session.delete( parent ); + } + ); + } + + @Test + public void testUpdateParentWithNewChildCommitWithReadOnlyParent(SessionFactoryScope scope) { + VersionedNode p = createVersionNode( "parent", "parent", scope ); + + clearCounts( scope ); + + p.setName( "new parent name" ); + VersionedNode c = new VersionedNode( "child", "child" ); + p.addChild( c ); + + scope.inTransaction( + session -> { + session.update( p ); + session.setReadOnly( p, true ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 1, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( parent.getName(), "parent" ); + assertEquals( 1, parent.getChildren().size() ); + assertEquals( 1, parent.getVersion() ); + assertSame( parent, child.getParent() ); + assertSame( child, parent.getChildren().iterator().next() ); + assertEquals( 0, child.getVersion() ); + session.setReadOnly( parent, true ); + session.setReadOnly( child, true ); + session.delete( parent ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 2, scope ); + } + + @Test + public void testMergeDetachedParentWithNewChildCommitWithReadOnlyParent(SessionFactoryScope scope) { + VersionedNode p = createVersionNode( "parent", "parent", scope ); + + clearCounts( scope ); + + p.setName( "new parent name" ); + VersionedNode c = new VersionedNode( "child", "child" ); + p.addChild( c ); + + scope.inTransaction( + session -> { + VersionedNode parent = (VersionedNode) session.merge( p ); + session.setReadOnly( parent, true ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 1, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( parent.getName(), "parent" ); + assertEquals( 1, parent.getChildren().size() ); + assertEquals( 1, parent.getVersion() ); + assertSame( parent, child.getParent() ); + assertSame( child, parent.getChildren().iterator().next() ); + assertEquals( 0, child.getVersion() ); + session.setReadOnly( parent, true ); + session.setReadOnly( child, true ); + session.delete( parent ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 2, scope ); + } + + @Test + public void testGetParentMakeReadOnlyThenMergeDetachedParentWithNewChildC(SessionFactoryScope scope) { + VersionedNode p = createVersionNode( "parent", "parent", scope ); + + clearCounts( scope ); + + p.setName( "new parent name" ); + VersionedNode c = new VersionedNode( "child", "child" ); + p.addChild( c ); + + scope.inTransaction( + session -> { + VersionedNode parentManaged = session.get( VersionedNode.class, p.getId() ); + session.setReadOnly( parentManaged, true ); + VersionedNode parentMerged = (VersionedNode) session.merge( p ); + assertSame( parentManaged, parentMerged ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 1, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( parent.getName(), "parent" ); + assertEquals( 1, parent.getChildren().size() ); + assertEquals( 1, parent.getVersion() ); + assertSame( parent, child.getParent() ); + assertSame( child, parent.getChildren().iterator().next() ); + assertEquals( 0, child.getVersion() ); + session.delete( parent ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 2, scope ); + } + + @Test + public void testMergeUnchangedDetachedParentChildren(SessionFactoryScope scope) { + VersionedNode p = new VersionedNode( "parent", "parent" ); + VersionedNode c = new VersionedNode( "child", "child" ); + scope.inTransaction( + session -> { + p.addChild( c ); + session.persist( p ); + } + ); + + clearCounts( scope ); + + VersionedNode parent = scope.fromTransaction( + session -> + (VersionedNode) session.merge( p ) + ); + + assertUpdateCount( 0, scope ); + assertInsertCount( 0, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parentGet = session.get( p.getClass(), p.getId() ); + session.merge( parent ); + } + ); + + assertUpdateCount( 0, scope ); + assertInsertCount( 0, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parentLoad = (VersionedNode) session.load( parent.getClass(), parent.getId() ); + session.merge( parent ); + } + ); + + assertUpdateCount( 0, scope ); + assertInsertCount( 0, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parent_ = session.get( VersionedNode.class, parent.getId() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( parent_.getName(), "parent" ); + assertEquals( 1, parent_.getChildren().size() ); + assertEquals( 0, parent_.getVersion() ); + assertSame( parent_, child.getParent() ); + assertSame( child, parent_.getChildren().iterator().next() ); + assertEquals( 0, child.getVersion() ); + session.delete( parent_ ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 2, scope ); + } + + @Test + public void testAddNewParentToReadOnlyChild(SessionFactoryScope scope) { + VersionedNode c = createVersionNode( "child", "child", scope ); + + clearCounts( scope ); + + VersionedNode p = new VersionedNode( "parent", "parent" ); + scope.inTransaction( + session -> { + VersionedNode childManaged = session.get( VersionedNode.class, c.getId() ); + session.setReadOnly( childManaged, true ); + childManaged.setName( "new child name" ); + p.addChild( childManaged ); + } + ); + + assertUpdateCount( 0, scope ); + assertInsertCount( 1, scope ); + + scope.inTransaction( + session -> { + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( "child", child.getName() ); + assertNull( child.getParent() ); + assertEquals( 0, child.getVersion() ); + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + assertNotNull( parent ); + session.setReadOnly( child, true ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 1, scope ); + } + + @Test + public void testUpdateChildWithNewParentCommitWithReadOnlyChild(SessionFactoryScope scope) { + VersionedNode c = createVersionNode( "child", "child", scope ); + + + clearCounts( scope ); + + c.setName( "new child name" ); + VersionedNode p = new VersionedNode( "parent", "parent" ); + p.addChild( c ); + + scope.inTransaction( + session -> { + session.update( c ); + session.setReadOnly( c, true ); + } + ); + + assertUpdateCount( 0, scope ); + assertInsertCount( 1, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( child.getName(), "child" ); + assertNull( child.getParent() ); + assertEquals( 0, child.getVersion() ); + assertNotNull( parent ); + assertEquals( 0, parent.getChildren().size() ); + assertEquals( 0, parent.getVersion() ); + session.setReadOnly( parent, true ); + session.setReadOnly( child, true ); + session.delete( parent ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 2, scope ); + } + + @Test + public void testMergeDetachedChildWithNewParentCommitWithReadOnlyChild(SessionFactoryScope scope) { + VersionedNode c = createVersionNode( "child", "child", scope ); + + clearCounts( scope ); + + c.setName( "new child name" ); + VersionedNode p = new VersionedNode( "parent", "parent" ); + p.addChild( c ); + + scope.inTransaction( + session -> { + VersionedNode child = (VersionedNode) session.merge( c ); + session.setReadOnly( child, true ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 1, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( child.getName(), "child" ); + assertNull( child.getParent() ); + assertEquals( 0, child.getVersion() ); + assertNotNull( parent ); + assertEquals( 0, parent.getChildren().size() ); + assertEquals( 1, parent.getVersion() ); // hmmm, why is was version updated? + session.setReadOnly( parent, true ); + session.setReadOnly( child, true ); + session.delete( parent ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 2, scope ); + } + + @Test + public void testGetChildMakeReadOnlyThenMergeDetachedChildWithNewParent(SessionFactoryScope scope) { + VersionedNode c = createVersionNode( "child", "child", scope ); + + clearCounts( scope ); + + c.setName( "new child name" ); + VersionedNode p = new VersionedNode( "parent", "parent" ); + p.addChild( c ); + + scope.inTransaction( + session -> { + VersionedNode childManaged = session.get( VersionedNode.class, c.getId() ); + session.setReadOnly( childManaged, true ); + VersionedNode childMerged = (VersionedNode) session.merge( c ); + assertSame( childManaged, childMerged ); + } + ); + + assertUpdateCount( 1, scope ); + assertInsertCount( 1, scope ); + clearCounts( scope ); + + scope.inTransaction( + session -> { + VersionedNode parent = session.get( VersionedNode.class, p.getId() ); + VersionedNode child = session.get( VersionedNode.class, c.getId() ); + assertEquals( child.getName(), "child" ); + assertNull( child.getParent() ); + assertEquals( 0, child.getVersion() ); + assertNotNull( parent ); + assertEquals( 0, parent.getChildren().size() ); + assertEquals( 1, parent.getVersion() ); // / hmmm, why is was version updated? + session.setReadOnly( parent, true ); + session.setReadOnly( child, true ); + session.delete( parent ); + session.delete( child ); + } + ); + + assertUpdateCount( 0, scope ); + assertDeleteCount( 2, scope ); + } + + protected void cleanupTest(SessionFactoryScope scope) throws Exception { + cleanup( scope ); + } + + private void cleanup(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from VersionedNode where parent is not null" ).executeUpdate(); + session.createQuery( "delete from VersionedNode" ).executeUpdate(); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/AbstractReadOnlyTest.java b/hibernate-core/src/test/java/org/hibernate/test/readonly/AbstractReadOnlyTest.java index 640dff21ef..a290ae710f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/AbstractReadOnlyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/readonly/AbstractReadOnlyTest.java @@ -7,50 +7,54 @@ package org.hibernate.test.readonly; import org.hibernate.CacheMode; -import org.hibernate.Session; -import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.hibernate.testing.orm.junit.SettingProvider; + +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; /** * @author Gail Badner */ -public abstract class AbstractReadOnlyTest extends BaseCoreFunctionalTestCase { - @Override - public void configure(Configuration cfg) { - cfg.setProperty( Environment.GENERATE_STATISTICS, "true"); - cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" ); +@SessionFactory( + generateStatistics = true +) +@ServiceRegistry( + settings = @Setting(name = Environment.STATEMENT_BATCH_SIZE, value = "0"), + settingProviders = @SettingProvider(settingName = AvailableSettings.JAKARTA_SHARED_CACHE_RETRIEVE_MODE, provider = AbstractReadOnlyTest.CacheModeProvider.class) +) +public abstract class AbstractReadOnlyTest { + + public static class CacheModeProvider implements SettingProvider.Provider { + + @Override + public CacheMode getSetting() { + return CacheMode.IGNORE; + } } - public Session openSession() { - Session s = super.openSession(); - s.setCacheMode( getSessionCacheMode() ); - return s; + protected void clearCounts(SessionFactoryScope scope) { + scope.getSessionFactory().getStatistics().clear(); } - protected CacheMode getSessionCacheMode() { - return CacheMode.IGNORE; + protected void assertInsertCount(int expected, SessionFactoryScope scope) { + int inserts = (int) scope.getSessionFactory().getStatistics().getEntityInsertCount(); + assertEquals( expected, inserts, "unexpected insert count" ); } - protected void clearCounts() { - sessionFactory().getStatistics().clear(); + protected void assertUpdateCount(int expected, SessionFactoryScope scope) { + int updates = (int) scope.getSessionFactory().getStatistics().getEntityUpdateCount(); + assertEquals( expected, updates, "unexpected update counts" ); } - protected void assertInsertCount(int expected) { - int inserts = ( int ) sessionFactory().getStatistics().getEntityInsertCount(); - assertEquals( "unexpected insert count", expected, inserts ); - } - - protected void assertUpdateCount(int expected) { - int updates = ( int ) sessionFactory().getStatistics().getEntityUpdateCount(); - assertEquals( "unexpected update counts", expected, updates ); - } - - protected void assertDeleteCount(int expected) { - int deletes = ( int ) sessionFactory().getStatistics().getEntityDeleteCount(); - assertEquals( "unexpected delete counts", expected, deletes ); + protected void assertDeleteCount(int expected, SessionFactoryScope scope) { + int deletes = (int) scope.getSessionFactory().getStatistics().getEntityDeleteCount(); + assertEquals( expected, deletes, "unexpected delete counts" ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java index 1f996663f7..d7ed76bf58 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java @@ -18,241 +18,256 @@ import org.hibernate.Transaction; import org.hibernate.proxy.HibernateProxy; import org.hibernate.query.Query; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ +@DomainModel( + xmlMappings = { + "org/hibernate/test/readonly/DataPoint.hbm.xml", + "org/hibernate/test/readonly/TextHolder.hbm.xml" + } +) public class ReadOnlySessionTest extends AbstractReadOnlyTest { - @Override - public String[] getMappings() { - return new String[] { "readonly/DataPoint.hbm.xml", "readonly/TextHolder.hbm.xml" }; + + @AfterEach + public void tearDown(SessionFactoryScope scope){ + scope.inTransaction( + session -> { + session.createQuery( "delete from DataPoint" ).executeUpdate(); + session.createQuery( "delete from TextHolder" ).executeUpdate(); + } + ); } @Test - public void testReadOnlyOnProxies() { - Session s = openSession(); + public void testReadOnlyOnProxies(SessionFactoryScope scope) { + Session s = openSession( scope ); s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal( 0.1d ).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); dp.setDescription( "original" ); s.save( dp ); long dpId = dp.getId(); s.getTransaction().commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.beginTransaction(); s.setDefaultReadOnly( true ); assertTrue( s.isDefaultReadOnly() ); - dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpId ) ); + dp = (DataPoint) s.load( DataPoint.class, new Long( dpId ) ); s.setDefaultReadOnly( false ); assertFalse( "was initialized", Hibernate.isInitialized( dp ) ); assertTrue( s.isReadOnly( dp ) ); assertFalse( "was initialized during isReadOnly", Hibernate.isInitialized( dp ) ); dp.setDescription( "changed" ); - assertTrue( "was not initialized during mod", Hibernate.isInitialized( dp ) ); - assertEquals( "desc not changed in memory", "changed", dp.getDescription() ); + assertTrue( Hibernate.isInitialized( dp ), "was not initialized during mod" ); + assertEquals( "changed", dp.getDescription(), "desc not changed in memory" ); s.flush(); s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); List list = s.createQuery( "from DataPoint where description = 'changed'" ).list(); - assertEquals( "change written to database", 0, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + assertEquals( 0, list.size() , "change written to database"); + s.createQuery( "delete from DataPoint" ).executeUpdate(); s.getTransaction().commit(); s.close(); } @Test - public void testReadOnlySessionDefaultQueryScroll() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlySessionDefaultQueryScroll(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); int i = 0; - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") - .scroll(ScrollMode.FORWARD_ONLY); + ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) + .scroll( ScrollMode.FORWARD_ONLY ); s.setDefaultReadOnly( false ); while ( sr.next() ) { DataPoint dp = (DataPoint) sr.get(); - if (++i==50) { - s.setReadOnly(dp, false); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } t.commit(); s.clear(); t = s.beginTransaction(); - List single = s.createQuery("from DataPoint where description='done!'").list(); + List single = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( 1, single.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } @Test - public void testReadOnlySessionModifiableQueryScroll() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlySessionModifiableQueryScroll(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); int i = 0; - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") + ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) .setReadOnly( false ) - .scroll(ScrollMode.FORWARD_ONLY); + .scroll( ScrollMode.FORWARD_ONLY ); while ( sr.next() ) { DataPoint dp = (DataPoint) sr.get(); - if (++i==50) { - s.setReadOnly(dp, true); + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } t.commit(); s.clear(); t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( 99, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } @Test - public void testModifiableSessionReadOnlyQueryScroll() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testModifiableSessionReadOnlyQueryScroll(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); assertFalse( s.isDefaultReadOnly() ); int i = 0; - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") + ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) .setReadOnly( true ) - .scroll(ScrollMode.FORWARD_ONLY); + .scroll( ScrollMode.FORWARD_ONLY ); while ( sr.next() ) { DataPoint dp = (DataPoint) sr.get(); - if (++i==50) { - s.setReadOnly(dp, false); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } t.commit(); s.clear(); t = s.beginTransaction(); - List single = s.createQuery("from DataPoint where description='done!'").list(); + List single = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( 1, single.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } @Test - public void testModifiableSessionDefaultQueryReadOnlySessionScroll() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testModifiableSessionDefaultQueryReadOnlySessionScroll(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( false ); int i = 0; - Query query = s.createQuery("from DataPoint dp order by dp.x asc"); + Query query = s.createQuery( "from DataPoint dp order by dp.x asc" ); s.setDefaultReadOnly( true ); - ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY); + ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY ); s.setDefaultReadOnly( false ); while ( sr.next() ) { DataPoint dp = (DataPoint) sr.get(); - if (++i==50) { - s.setReadOnly(dp, false); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } t.commit(); s.clear(); t = s.beginTransaction(); - List single = s.createQuery("from DataPoint where description='done!'").list(); + List single = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( 1, single.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } @Test - public void testQueryReadOnlyScroll() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testQueryReadOnlyScroll(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = null; - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( false ); int i = 0; - Query query = s.createQuery("from DataPoint dp order by dp.x asc"); + Query query = s.createQuery( "from DataPoint dp order by dp.x asc" ); assertFalse( query.isReadOnly() ); s.setDefaultReadOnly( true ); assertTrue( query.isReadOnly() ); @@ -272,10 +287,10 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { assertTrue( query.isReadOnly() ); s.setDefaultReadOnly( false ); assertFalse( s.isDefaultReadOnly() ); - ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY); + ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY ); assertFalse( s.isDefaultReadOnly() ); assertTrue( query.isReadOnly() ); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertFalse( s.isReadOnly( dpLast ) ); query.setReadOnly( false ); assertFalse( query.isReadOnly() ); @@ -291,44 +306,44 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { else { assertTrue( s.isReadOnly( dp ) ); } - if (++i==50) { - s.setReadOnly(dp, false); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); nExpectedChanges = ( dp == dpLast ? 1 : 2 ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } assertFalse( s.isDefaultReadOnly() ); t.commit(); s.clear(); t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( nExpectedChanges, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } @Test - public void testQueryModifiableScroll() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testQueryModifiableScroll(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = null; - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); int i = 0; - Query query = s.createQuery("from DataPoint dp order by dp.x asc"); + Query query = s.createQuery( "from DataPoint dp order by dp.x asc" ); assertTrue( query.isReadOnly() ); s.setDefaultReadOnly( false ); assertFalse( query.isReadOnly() ); @@ -348,9 +363,9 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { assertFalse( query.isReadOnly() ); s.setDefaultReadOnly( true ); assertTrue( s.isDefaultReadOnly() ); - ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY); + ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY ); assertFalse( query.isReadOnly() ); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertTrue( s.isReadOnly( dpLast ) ); query.setReadOnly( true ); assertTrue( query.isReadOnly() ); @@ -366,48 +381,41 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { else { assertFalse( s.isReadOnly( dp ) ); } - if (++i==50) { - s.setReadOnly(dp, true); + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); nExpectedChanges = ( dp == dpLast ? 99 : 98 ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } assertTrue( s.isDefaultReadOnly() ); t.commit(); s.clear(); t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( nExpectedChanges, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } - - - - - - - @Test - public void testReadOnlyRefresh() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyRefresh(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); s.setDefaultReadOnly( true ); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertTrue( s.isReadOnly( dp ) ); assertEquals( "original", dp.getDescription() ); dp.setDescription( "changed" ); @@ -427,7 +435,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { s.clear(); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertEquals( "original", dp.getDescription() ); s.delete( dp ); t.commit(); @@ -435,20 +443,20 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyRefreshDetached() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyRefreshDetached(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( false ); dp.setDescription( "changed" ); @@ -474,7 +482,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { s.clear(); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertEquals( "original", dp.getDescription() ); s.delete( dp ); t.commit(); @@ -482,23 +490,23 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyProxyRefresh() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyProxyRefresh(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); - dp = ( DataPoint ) s.load( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.load( DataPoint.class, dp.getId() ); assertTrue( s.isReadOnly( dp ) ); assertFalse( Hibernate.isInitialized( dp ) ); s.refresh( dp ); @@ -513,24 +521,24 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { dp.setDescription( "changed" ); assertEquals( "changed", dp.getDescription() ); assertTrue( s.isReadOnly( dp ) ); - assertTrue( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertTrue( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); s.refresh( dp ); assertEquals( "original", dp.getDescription() ); assertTrue( s.isReadOnly( dp ) ); - assertTrue( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertTrue( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); s.setDefaultReadOnly( true ); dp.setDescription( "changed" ); assertEquals( "changed", dp.getDescription() ); s.refresh( dp ); assertTrue( s.isReadOnly( dp ) ); - assertTrue( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertTrue( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); assertEquals( "original", dp.getDescription() ); dp.setDescription( "changed" ); t.commit(); s.clear(); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertEquals( "original", dp.getDescription() ); s.delete( dp ); t.commit(); @@ -539,23 +547,23 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyProxyRefreshDetached() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyProxyRefreshDetached(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); - dp = ( DataPoint ) s.load( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.load( DataPoint.class, dp.getId() ); assertFalse( Hibernate.isInitialized( dp ) ); assertTrue( s.isReadOnly( dp ) ); s.evict( dp ); @@ -567,7 +575,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { s.refresh( dp ); assertFalse( Hibernate.isInitialized( dp ) ); assertFalse( s.isReadOnly( dp ) ); - assertFalse( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertFalse( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); dp.setDescription( "changed" ); assertEquals( "changed", dp.getDescription() ); assertTrue( Hibernate.isInitialized( dp ) ); @@ -575,7 +583,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { s.refresh( dp ); assertEquals( "original", dp.getDescription() ); assertFalse( s.isReadOnly( dp ) ); - assertFalse( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertFalse( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); dp.setDescription( "changed" ); assertEquals( "changed", dp.getDescription() ); s.setDefaultReadOnly( true ); @@ -583,14 +591,14 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { s.refresh( dp ); assertEquals( "original", dp.getDescription() ); assertTrue( s.isReadOnly( dp ) ); - assertTrue( s.isReadOnly( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getImplementation() ) ); + assertTrue( s.isReadOnly( ( (HibernateProxy) dp ).getHibernateLazyInitializer().getImplementation() ) ); dp.setDescription( "changed" ); assertEquals( "changed", dp.getDescription() ); t.commit(); s.clear(); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertEquals( "original", dp.getDescription() ); s.delete( dp ); t.commit(); @@ -598,31 +606,31 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyDelete() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyDelete(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.setDefaultReadOnly( true ); - s.setCacheMode(CacheMode.IGNORE); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); s.setDefaultReadOnly( false ); assertTrue( s.isReadOnly( dp ) ); - s.delete( dp ); + s.delete( dp ); t.commit(); s.close(); - s = openSession(); + s = openSession( scope ); t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where id=" + dp.getId() ).list(); + List list = s.createQuery( "from DataPoint where id=" + dp.getId() ).list(); assertTrue( list.isEmpty() ); t.commit(); s.close(); @@ -630,60 +638,60 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { } @Test - public void testReadOnlyGetModifyAndDelete() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyGetModifyAndDelete(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.setDefaultReadOnly( true ); - s.setCacheMode(CacheMode.IGNORE); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + dp = (DataPoint) s.get( DataPoint.class, dp.getId() ); s.setDefaultReadOnly( true ); dp.setDescription( "a DataPoint" ); - s.delete( dp ); + s.delete( dp ); t.commit(); s.close(); - s = openSession(); + s = openSession( scope ); t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where id=" + dp.getId() ).list(); + List list = s.createQuery( "from DataPoint where id=" + dp.getId() ).list(); assertTrue( list.isEmpty() ); t.commit(); s.close(); } @Test - public void testReadOnlyModeWithExistingModifiableEntity() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testReadOnlyModeWithExistingModifiableEntity(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = null; - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertFalse( s.isReadOnly( dpLast ) ); s.setDefaultReadOnly( true ); int i = 0; - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") - .scroll(ScrollMode.FORWARD_ONLY); + ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) + .scroll( ScrollMode.FORWARD_ONLY ); s.setDefaultReadOnly( false ); int nExpectedChanges = 0; while ( sr.next() ) { @@ -695,47 +703,47 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { else { assertTrue( s.isReadOnly( dp ) ); } - if (++i==50) { - s.setReadOnly(dp, false); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); nExpectedChanges = ( dp == dpLast ? 1 : 2 ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } t.commit(); s.clear(); t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( nExpectedChanges, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } @Test - public void testModifiableModeWithExistingReadOnlyEntity() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testModifiableModeWithExistingReadOnlyEntity(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = null; - for ( int i=0; i<100; i++ ) { + for ( int i = 0; i < 100; i++ ) { dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); } t.commit(); s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); assertTrue( s.isReadOnly( dpLast ) ); int i = 0; - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") - .setReadOnly(false) - .scroll(ScrollMode.FORWARD_ONLY); + ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) + .setReadOnly( false ) + .scroll( ScrollMode.FORWARD_ONLY ); int nExpectedChanges = 0; while ( sr.next() ) { dp = (DataPoint) sr.get(); @@ -746,28 +754,28 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { else { assertFalse( s.isReadOnly( dp ) ); } - if (++i==50) { - s.setReadOnly(dp, true); + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); nExpectedChanges = ( dp == dpLast ? 99 : 98 ); } - dp.setDescription("done!"); + dp.setDescription( "done!" ); } t.commit(); s.clear(); t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); + List list = s.createQuery( "from DataPoint where description='done!'" ).list(); assertEquals( nExpectedChanges, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); + s.createQuery( "delete from DataPoint" ).executeUpdate(); t.commit(); s.close(); } @Test - public void testReadOnlyOnTextType() { + public void testReadOnlyOnTextType(SessionFactoryScope scope) { final String origText = "some huge text string"; final String newText = "some even bigger text string"; - Session s = openSession(); + Session s = openSession( scope ); s.beginTransaction(); s.setCacheMode( CacheMode.IGNORE ); TextHolder holder = new TextHolder( origText ); @@ -776,53 +784,53 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); s.setDefaultReadOnly( true ); s.setCacheMode( CacheMode.IGNORE ); - holder = ( TextHolder ) s.get( TextHolder.class, id ); + holder = (TextHolder) s.get( TextHolder.class, id ); s.setDefaultReadOnly( false ); holder.setTheText( newText ); s.flush(); s.getTransaction().commit(); s.close(); - s = openSession(); + s = openSession( scope ); s.beginTransaction(); - holder = ( TextHolder ) s.get( TextHolder.class, id ); - assertEquals( "change written to database", origText, holder.getTheText() ); + holder = (TextHolder) s.get( TextHolder.class, id ); + assertEquals( origText, holder.getTheText() , "change written to database"); s.delete( holder ); s.getTransaction().commit(); s.close(); } @Test - public void testMergeWithReadOnlyEntity() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testMergeWithReadOnlyEntity(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); dp.setDescription( "description" ); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); - DataPoint dpManaged = ( DataPoint ) s.get( DataPoint.class, new Long( dp.getId() ) ); - DataPoint dpMerged = ( DataPoint ) s.merge( dp ); + DataPoint dpManaged = (DataPoint) s.get( DataPoint.class, new Long( dp.getId() ) ); + DataPoint dpMerged = (DataPoint) s.merge( dp ); assertSame( dpManaged, dpMerged ); t.commit(); s.close(); - s = openSession(); + s = openSession( scope ); t = s.beginTransaction(); - dpManaged = ( DataPoint ) s.get( DataPoint.class, new Long( dp.getId() ) ); + dpManaged = (DataPoint) s.get( DataPoint.class, new Long( dp.getId() ) ); assertNull( dpManaged.getDescription() ); s.delete( dpManaged ); t.commit(); @@ -831,54 +839,58 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { } @Test - public void testMergeWithReadOnlyProxy() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + public void testMergeWithReadOnlyProxy(SessionFactoryScope scope) { + Session s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); + dp.setX( new BigDecimal( 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); t.commit(); s.close(); dp.setDescription( "description" ); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); + s = openSession( scope ); + s.setCacheMode( CacheMode.IGNORE ); t = s.beginTransaction(); s.setDefaultReadOnly( true ); - DataPoint dpProxy = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) ); + DataPoint dpProxy = (DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) ); assertTrue( s.isReadOnly( dpProxy ) ); assertFalse( Hibernate.isInitialized( dpProxy ) ); s.evict( dpProxy ); - dpProxy = ( DataPoint ) s.merge( dpProxy ); + dpProxy = (DataPoint) s.merge( dpProxy ); assertTrue( s.isReadOnly( dpProxy ) ); assertFalse( Hibernate.isInitialized( dpProxy ) ); - dpProxy = ( DataPoint ) s.merge( dp ); + dpProxy = (DataPoint) s.merge( dp ); assertTrue( s.isReadOnly( dpProxy ) ); assertTrue( Hibernate.isInitialized( dpProxy ) ); assertEquals( "description", dpProxy.getDescription() ); s.evict( dpProxy ); - dpProxy = ( DataPoint ) s.merge( dpProxy ); + dpProxy = (DataPoint) s.merge( dpProxy ); assertTrue( s.isReadOnly( dpProxy ) ); assertTrue( Hibernate.isInitialized( dpProxy ) ); assertEquals( "description", dpProxy.getDescription() ); dpProxy.setDescription( null ); - dpProxy = ( DataPoint ) s.merge( dp ); + dpProxy = (DataPoint) s.merge( dp ); assertTrue( s.isReadOnly( dpProxy ) ); assertTrue( Hibernate.isInitialized( dpProxy ) ); - assertEquals( "description", dpProxy.getDescription() ); + assertEquals( "description", dpProxy.getDescription() ); t.commit(); s.close(); - s = openSession(); + s = openSession( scope ); t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, new Long( dp.getId() ) ); + dp = (DataPoint) s.get( DataPoint.class, new Long( dp.getId() ) ); assertNull( dp.getDescription() ); s.delete( dp ); t.commit(); s.close(); } + + private Session openSession(SessionFactoryScope scope) { + return scope.getSessionFactory().openSession(); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyTest.java b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyTest.java deleted file mode 100644 index 4f65a37588..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyTest.java +++ /dev/null @@ -1,603 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.readonly; - -import java.math.BigDecimal; -import java.util.List; - -import org.hibernate.Hibernate; -import org.hibernate.ScrollMode; -import org.hibernate.ScrollableResults; -import org.hibernate.Session; -import org.hibernate.Transaction; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - -/** - * - * @author Gavin King - * @author Gail Badner - */ -public class ReadOnlyTest extends AbstractReadOnlyTest { - @Override - public String[] getMappings() { - return new String[] { "readonly/DataPoint.hbm.xml", "readonly/TextHolder.hbm.xml" }; - } - - @Test - public void testReadOnlyOnProxies() { - clearCounts(); - - Session s = openSession(); - s.beginTransaction(); - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal( 0.1d ).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setDescription( "original" ); - s.save( dp ); - long dpId = dp.getId(); - s.getTransaction().commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpId ) ); - assertFalse( "was initialized", Hibernate.isInitialized( dp ) ); - s.setReadOnly( dp, true ); - assertFalse( "was initialized during setReadOnly", Hibernate.isInitialized( dp ) ); - dp.setDescription( "changed" ); - assertTrue( "was not initialized during mod", Hibernate.isInitialized( dp ) ); - assertEquals( "desc not changed in memory", "changed", dp.getDescription() ); - s.flush(); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - - s = openSession(); - s.beginTransaction(); - List list = s.createQuery( "from DataPoint where description = 'changed'" ).list(); - assertEquals( "change written to database", 0, list.size() ); - assertEquals( 1, s.createQuery("delete from DataPoint").executeUpdate() ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - //deletes from Query.executeUpdate() are not tracked - //assertDeleteCount( 1 ); - } - - @Test - public void testReadOnlyMode() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - - assertInsertCount( 100 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - int i = 0; - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") - .setReadOnly(true) - .scroll(ScrollMode.FORWARD_ONLY); - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - if (++i==50) { - s.setReadOnly(dp, false); - } - dp.setDescription("done!"); - } - t.commit(); - - assertUpdateCount( 1 ); - clearCounts(); - - s.clear(); - t = s.beginTransaction(); - List single = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( single.size(), 1 ); - assertEquals( 100, s.createQuery("delete from DataPoint").executeUpdate() ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - //deletes from Query.executeUpdate() are not tracked - //assertDeleteCount( 100 ); - } - - @Test - public void testReadOnlyModeAutoFlushOnQuery() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dpFirst = null; - for ( int i=0; i<100; i++ ) { - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - - assertInsertCount( 0 ); - assertUpdateCount( 0 ); - - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") - .setReadOnly(true) - .scroll(ScrollMode.FORWARD_ONLY); - - assertInsertCount( 100 ); - assertUpdateCount( 0 ); - clearCounts(); - - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - assertFalse( s.isReadOnly( dp ) ); - s.delete( dp ); - } - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 100 ); - } - - @Test - public void testSaveReadOnlyModifyInSaveTransaction() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = new DataPoint(); - dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - s.setReadOnly( dp, true ); - dp.setDescription( "different" ); - t.commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - s.setReadOnly( dp, true ); - assertEquals( "original", dp.getDescription() ); - dp.setDescription( "changed" ); - assertEquals( "changed", dp.getDescription() ); - s.refresh( dp ); - assertEquals( "original", dp.getDescription() ); - dp.setDescription( "changed" ); - assertEquals( "changed", dp.getDescription() ); - t.commit(); - - assertInsertCount( 0 ); - assertUpdateCount( 0 ); - - s.clear(); - t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - assertEquals( "original", dp.getDescription() ); - s.delete( dp ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - clearCounts(); - } - - @Test - public void testReadOnlyRefresh() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = new DataPoint(); - dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - t.commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - s.setReadOnly( dp, true ); - assertEquals( "original", dp.getDescription() ); - dp.setDescription( "changed" ); - assertEquals( "changed", dp.getDescription() ); - s.refresh( dp ); - assertEquals( "original", dp.getDescription() ); - dp.setDescription( "changed" ); - assertEquals( "changed", dp.getDescription() ); - t.commit(); - - assertInsertCount( 0 ); - assertUpdateCount( 0 ); - - s.clear(); - t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - assertEquals( "original", dp.getDescription() ); - s.delete( dp ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - clearCounts(); - } - - @Test - public void testReadOnlyRefreshDetached() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = new DataPoint(); - dp.setDescription( "original" ); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - t.commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - dp.setDescription( "changed" ); - assertEquals( "changed", dp.getDescription() ); - s.refresh( dp ); - assertEquals( "original", dp.getDescription() ); - assertFalse( s.isReadOnly( dp ) ); - s.setReadOnly( dp, true ); - dp.setDescription( "changed" ); - assertEquals( "changed", dp.getDescription() ); - s.evict( dp ); - s.refresh( dp ); - assertEquals( "original", dp.getDescription() ); - assertFalse( s.isReadOnly( dp ) ); - t.commit(); - - assertInsertCount( 0 ); - assertUpdateCount( 0 ); - - s.clear(); - t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - assertEquals( "original", dp.getDescription() ); - s.delete( dp ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - } - - @Test - public void testReadOnlyDelete() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - t.commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - s.setReadOnly( dp, true ); - s.delete( dp ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - - s = openSession(); - t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); - assertTrue( list.isEmpty() ); - t.commit(); - s.close(); - - } - - @Test - public void testReadOnlyGetModifyAndDelete() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - t.commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - dp = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - s.setReadOnly( dp, true ); - dp.setDescription( "a DataPoint" ); - s.delete( dp ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); - assertTrue( list.isEmpty() ); - t.commit(); - s.close(); - - } - - @Test - public void testReadOnlyModeWithExistingModifiableEntity() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = null; - for ( int i=0; i<100; i++ ) { - dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - - assertInsertCount( 100 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - assertFalse( s.isReadOnly( dpLast ) ); - int i = 0; - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") - .setReadOnly(true) - .scroll(ScrollMode.FORWARD_ONLY); - int nExpectedChanges = 0; - while ( sr.next() ) { - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertFalse( s.isReadOnly( dp ) ); - } - else { - assertTrue( s.isReadOnly( dp ) ); - } - if (++i==50) { - s.setReadOnly(dp, false); - nExpectedChanges = ( dp == dpLast ? 1 : 2 ); - } - dp.setDescription("done!"); - } - t.commit(); - s.clear(); - - assertInsertCount( 0 ); - assertUpdateCount( nExpectedChanges ); - clearCounts(); - - t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( list.size(), nExpectedChanges ); - assertEquals( 100, s.createQuery("delete from DataPoint").executeUpdate() ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - } - - @Test - public void testModifiableModeWithExistingReadOnlyEntity() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = null; - for ( int i=0; i<100; i++ ) { - dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - - assertInsertCount( 100 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - t = s.beginTransaction(); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - assertFalse( s.isReadOnly( dpLast ) ); - s.setReadOnly( dpLast, true ); - assertTrue( s.isReadOnly( dpLast ) ); - dpLast.setDescription( "oy" ); - int i = 0; - - assertUpdateCount( 0 ); - - ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") - .setReadOnly(false) - .scroll(ScrollMode.FORWARD_ONLY); - int nExpectedChanges = 0; - while ( sr.next() ) { - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertTrue( s.isReadOnly( dp ) ); - } - else { - assertFalse( s.isReadOnly( dp ) ); - } - if (++i==50) { - s.setReadOnly(dp, true); - nExpectedChanges = ( dp == dpLast ? 99 : 98 ); - } - dp.setDescription("done!"); - } - t.commit(); - s.clear(); - - assertUpdateCount( nExpectedChanges ); - clearCounts(); - - t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( list.size(), nExpectedChanges ); - assertEquals( 100, s.createQuery("delete from DataPoint").executeUpdate() ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - } - - @Test - public void testReadOnlyOnTextType() { - final String origText = "some huge text string"; - final String newText = "some even bigger text string"; - - clearCounts(); - - Session s = openSession(); - s.beginTransaction(); - TextHolder holder = new TextHolder( origText ); - s.save( holder ); - Long id = holder.getId(); - s.getTransaction().commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - holder = ( TextHolder ) s.get( TextHolder.class, id ); - s.setReadOnly( holder, true ); - holder.setTheText( newText ); - s.flush(); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - - s = openSession(); - s.beginTransaction(); - holder = ( TextHolder ) s.get( TextHolder.class, id ); - assertEquals( "change written to database", origText, holder.getTheText() ); - s.delete( holder ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - } - - @Test - public void testMergeWithReadOnlyEntity() { - clearCounts(); - - Session s = openSession(); - Transaction t = s.beginTransaction(); - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - t.commit(); - s.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 0 ); - clearCounts(); - - dp.setDescription( "description" ); - - s = openSession(); - t = s.beginTransaction(); - DataPoint dpManaged = ( DataPoint ) s.get( DataPoint.class, new Long( dp.getId() ) ); - s.setReadOnly( dpManaged, true ); - DataPoint dpMerged = ( DataPoint ) s.merge( dp ); - assertSame( dpManaged, dpMerged ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - - s = openSession(); - t = s.beginTransaction(); - dpManaged = ( DataPoint ) s.get( DataPoint.class, new Long( dp.getId() ) ); - assertNull( dpManaged.getDescription() ); - s.delete( dpManaged ); - t.commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - } -} - diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyVersionedNodesTest.java b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyVersionedNodesTest.java deleted file mode 100644 index 55773e6a70..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyVersionedNodesTest.java +++ /dev/null @@ -1,689 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.readonly; - -import org.hibernate.Session; - -import org.hibernate.testing.FailureExpected; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; - -/** - * @author Gail Badner - */ -public class ReadOnlyVersionedNodesTest extends AbstractReadOnlyTest { - @Override - public String[] getMappings() { - return new String[] { "readonly/VersionedNode.hbm.xml" }; - } - - @Test - public void testSetReadOnlyTrueAndFalse() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode node = new VersionedNode( "node", "node" ); - s.persist( node ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - s.setReadOnly( node, true ); - node.setName( "node-name" ); - s.getTransaction().commit(); - - assertUpdateCount( 0 ); - assertInsertCount( 0 ); - - // the changed name is still in node - assertEquals( "node-name", node.getName() ); - - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - // the changed name is still in the session - assertEquals( "node-name", node.getName() ); - s.refresh( node ); - // after refresh, the name reverts to the original value - assertEquals( "node", node.getName() ); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - assertEquals( "node", node.getName() ); - s.getTransaction().commit(); - - s.close(); - - assertUpdateCount( 0 ); - assertInsertCount( 0 ); - - s = openSession(); - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - assertEquals( "node", node.getName() ); - s.setReadOnly( node, true ); - node.setName( "diff-node-name" ); - s.flush(); - assertEquals( "diff-node-name", node.getName() ); - s.refresh( node ); - assertEquals( "node", node.getName() ); - s.setReadOnly( node, false ); - node.setName( "diff-node-name" ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 0 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - assertEquals( "diff-node-name", node.getName() ); - assertEquals( 1, node.getVersion() ); - s.setReadOnly( node, true ); - s.delete( node ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - } - - @Test - public void testUpdateSetReadOnlyTwice() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode node = new VersionedNode( "node", "node" ); - s.persist( node ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - node.setName( "node-name" ); - s.setReadOnly( node, true ); - s.setReadOnly( node, true ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertInsertCount( 0 ); - - s = openSession(); - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - assertEquals( "node", node.getName() ); - assertEquals( 0, node.getVersion() ); - s.setReadOnly( node, true ); - s.delete( node ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - } - - @Test - public void testUpdateSetModifiable() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode node = new VersionedNode( "node", "node" ); - s.persist( node ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - node.setName( "node-name" ); - s.setReadOnly( node, false ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 0 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - assertEquals( "node-name", node.getName() ); - assertEquals( 1, node.getVersion() ); - s.setReadOnly( node, true ); - s.delete( node ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - } - - @Test - @FailureExpected( jiraKey = "unknown" ) - public void testUpdateSetReadOnlySetModifiable() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode node = new VersionedNode( "node", "node" ); - s.persist( node ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - node.setName( "node-name" ); - s.setReadOnly( node, true ); - s.setReadOnly( node, false ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 0 ); - - s = openSession(); - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - assertEquals( "node-name", node.getName() ); - assertEquals( 1, node.getVersion() ); - s.delete( node ); - s.getTransaction().commit(); - s.close(); - } - - @Test - @FailureExpected( jiraKey = "unknown" ) - public void testSetReadOnlyUpdateSetModifiable() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode node = new VersionedNode( "node", "node" ); - s.persist( node ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - s.setReadOnly( node, true ); - node.setName( "node-name" ); - s.setReadOnly( node, false ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 0 ); - - s = openSession(); - s.beginTransaction(); - node = ( VersionedNode ) s.get( VersionedNode.class, node.getId() ); - assertEquals( "node-name", node.getName() ); - assertEquals( 1, node.getVersion() ); - s.delete( node ); - s.getTransaction().commit(); - s.close(); - } - - @Test - public void testAddNewChildToReadOnlyParent() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode parent = new VersionedNode( "parent", "parent" ); - s.persist( parent ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - s.beginTransaction(); - VersionedNode parentManaged = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - s.setReadOnly( parentManaged, true ); - parentManaged.setName( "new parent name" ); - VersionedNode child = new VersionedNode( "child", "child"); - parentManaged.addChild( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 1 ); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - assertEquals( "parent", parent.getName() ); - assertEquals( 1, parent.getChildren().size() ); - assertEquals( 1, parent.getVersion() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertNotNull( child ); - s.delete( parent ); - s.getTransaction().commit(); - s.close(); - } - - @Test - public void testUpdateParentWithNewChildCommitWithReadOnlyParent() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode parent = new VersionedNode( "parent", "parent" ); - s.persist( parent ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - parent.setName( "new parent name" ); - VersionedNode child = new VersionedNode( "child", "child"); - parent.addChild( child ); - - s = openSession(); - s.beginTransaction(); - s.update( parent ); - s.setReadOnly( parent, true ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 1 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( parent.getName(), "parent" ); - assertEquals( 1, parent.getChildren().size() ); - assertEquals( 1, parent.getVersion() ); - assertSame( parent, child.getParent() ); - assertSame( child, parent.getChildren().iterator().next() ); - assertEquals( 0, child.getVersion() ); - s.setReadOnly( parent, true ); - s.setReadOnly( child, true ); - s.delete( parent ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 2 ); - } - - @Test - public void testMergeDetachedParentWithNewChildCommitWithReadOnlyParent() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode parent = new VersionedNode( "parent", "parent" ); - s.persist( parent ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - parent.setName( "new parent name" ); - VersionedNode child = new VersionedNode( "child", "child"); - parent.addChild( child ); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.merge( parent ); - s.setReadOnly( parent, true ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 1 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( parent.getName(), "parent" ); - assertEquals( 1, parent.getChildren().size() ); - assertEquals( 1, parent.getVersion() ); - assertSame( parent, child.getParent() ); - assertSame( child, parent.getChildren().iterator().next() ); - assertEquals( 0, child.getVersion() ); - s.setReadOnly( parent, true ); - s.setReadOnly( child, true ); - s.delete( parent ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 2 ); - } - - @Test - public void testGetParentMakeReadOnlyThenMergeDetachedParentWithNewChildC() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode parent = new VersionedNode( "parent", "parent" ); - s.persist( parent ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - parent.setName( "new parent name" ); - VersionedNode child = new VersionedNode( "child", "child"); - parent.addChild( child ); - - s = openSession(); - s.beginTransaction(); - VersionedNode parentManaged = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - s.setReadOnly( parentManaged, true ); - VersionedNode parentMerged = ( VersionedNode ) s.merge( parent ); - assertSame( parentManaged, parentMerged ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 1 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( parent.getName(), "parent" ); - assertEquals( 1, parent.getChildren().size() ); - assertEquals( 1, parent.getVersion() ); - assertSame( parent, child.getParent() ); - assertSame( child, parent.getChildren().iterator().next() ); - assertEquals( 0, child.getVersion() ); - s.delete( parent ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 2 ); - } - - @Test - public void testMergeUnchangedDetachedParentChildren() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode parent = new VersionedNode( "parent", "parent" ); - VersionedNode child = new VersionedNode( "child", "child"); - parent.addChild( child ); - s.persist( parent ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.merge( parent ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertInsertCount( 0 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - VersionedNode parentGet = ( VersionedNode ) s.get( parent.getClass(), parent.getId() ); - s.merge( parent ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertInsertCount( 0 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - VersionedNode parentLoad = ( VersionedNode ) s.load( parent.getClass(), parent.getId() ); - s.merge( parent ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertInsertCount( 0 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( parent.getName(), "parent" ); - assertEquals( 1, parent.getChildren().size() ); - assertEquals( 0, parent.getVersion() ); - assertSame( parent, child.getParent() ); - assertSame( child, parent.getChildren().iterator().next() ); - assertEquals( 0, child.getVersion() ); - s.delete( parent ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 2 ); - } - - @Test - public void testAddNewParentToReadOnlyChild() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode child = new VersionedNode( "child", "child" ); - s.persist( child ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - s = openSession(); - s.beginTransaction(); - VersionedNode childManaged = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - s.setReadOnly( childManaged, true ); - childManaged.setName( "new child name" ); - VersionedNode parent = new VersionedNode( "parent", "parent"); - parent.addChild( childManaged ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertInsertCount( 1 ); - - s = openSession(); - s.beginTransaction(); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( "child", child.getName() ); - assertNull( child.getParent() ); - assertEquals( 0, child.getVersion() ); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - assertNotNull( parent ); - s.setReadOnly( child, true ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 1 ); - } - - @Test - public void testUpdateChildWithNewParentCommitWithReadOnlyChild() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode child = new VersionedNode( "child", "child" ); - s.persist( child ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - child.setName( "new child name" ); - VersionedNode parent = new VersionedNode( "parent", "parent"); - parent.addChild( child ); - - s = openSession(); - s.beginTransaction(); - s.update( child ); - s.setReadOnly( child, true ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertInsertCount( 1 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( child.getName(), "child" ); - assertNull( child.getParent() ); - assertEquals( 0, child.getVersion() ); - assertNotNull( parent ); - assertEquals( 0, parent.getChildren().size() ); - assertEquals( 0, parent.getVersion() ); - s.setReadOnly( parent, true ); - s.setReadOnly( child, true ); - s.delete( parent ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 2 ); - } - - @Test - public void testMergeDetachedChildWithNewParentCommitWithReadOnlyChild() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode child = new VersionedNode( "child", "child" ); - s.persist( child ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - child.setName( "new child name" ); - VersionedNode parent = new VersionedNode( "parent", "parent"); - parent.addChild( child ); - - s = openSession(); - s.beginTransaction(); - child = ( VersionedNode ) s.merge( child ); - s.setReadOnly( child, true ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 1 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( child.getName(), "child" ); - assertNull( child.getParent() ); - assertEquals( 0, child.getVersion() ); - assertNotNull( parent ); - assertEquals( 0, parent.getChildren().size() ); - assertEquals( 1, parent.getVersion() ); // hmmm, why is was version updated? - s.setReadOnly( parent, true ); - s.setReadOnly( child, true ); - s.delete( parent ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 2 ); - } - - @Test - public void testGetChildMakeReadOnlyThenMergeDetachedChildWithNewParent() throws Exception { - Session s = openSession(); - s.beginTransaction(); - VersionedNode child = new VersionedNode( "child", "child" ); - s.persist( child ); - s.getTransaction().commit(); - s.close(); - - clearCounts(); - - child.setName( "new child name" ); - VersionedNode parent = new VersionedNode( "parent", "parent"); - parent.addChild( child ); - - s = openSession(); - s.beginTransaction(); - VersionedNode childManaged = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - s.setReadOnly( childManaged, true ); - VersionedNode childMerged = ( VersionedNode ) s.merge( child ); - assertSame( childManaged, childMerged ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 1 ); - assertInsertCount( 1 ); - clearCounts(); - - s = openSession(); - s.beginTransaction(); - parent = ( VersionedNode ) s.get( VersionedNode.class, parent.getId() ); - child = ( VersionedNode ) s.get( VersionedNode.class, child.getId() ); - assertEquals( child.getName(), "child" ); - assertNull( child.getParent() ); - assertEquals( 0, child.getVersion() ); - assertNotNull( parent ); - assertEquals( 0, parent.getChildren().size() ); - assertEquals( 1, parent.getVersion() ); // / hmmm, why is was version updated? - s.setReadOnly( parent, true ); - s.setReadOnly( child, true ); - s.delete( parent ); - s.delete( child ); - s.getTransaction().commit(); - s.close(); - - assertUpdateCount( 0 ); - assertDeleteCount( 2 ); - } - - protected void cleanupTest() throws Exception { - cleanup(); - super.cleanupTest(); - } - - private void cleanup() { - Session s = sessionFactory().openSession(); - s.beginTransaction(); - - s.createQuery( "delete from VersionedNode where parent is not null" ).executeUpdate(); - s.createQuery( "delete from VersionedNode" ).executeUpdate(); - - s.getTransaction().commit(); - s.close(); - } -}