diff --git a/core/src/main/java/org/hibernate/engine/EntityEntry.java b/core/src/main/java/org/hibernate/engine/EntityEntry.java index 3cb67caf6c..0158265a88 100644 --- a/core/src/main/java/org/hibernate/engine/EntityEntry.java +++ b/core/src/main/java/org/hibernate/engine/EntityEntry.java @@ -262,6 +262,11 @@ public final class EntityEntry implements Serializable { } public void setReadOnly(boolean readOnly, Object entity) { + if ( ( readOnly && status == Status.READ_ONLY ) || + ( ( ! readOnly ) && status == Status.MANAGED ) ) { + // simply return since the status is not being changed + return; + } if (status!=Status.MANAGED && status!=Status.READ_ONLY) { throw new HibernateException("instance was not in a valid state"); } diff --git a/testsuite/src/test/java/org/hibernate/test/readonly/ReadOnlyVersionedNodesTest.java b/testsuite/src/test/java/org/hibernate/test/readonly/ReadOnlyVersionedNodesTest.java index 9e862ec92c..1b1dd0ef02 100644 --- a/testsuite/src/test/java/org/hibernate/test/readonly/ReadOnlyVersionedNodesTest.java +++ b/testsuite/src/test/java/org/hibernate/test/readonly/ReadOnlyVersionedNodesTest.java @@ -127,6 +127,71 @@ public class ReadOnlyVersionedNodesTest extends FunctionalTestCase { s.close(); } + public void testSetUpdateReadOnlyTwice() 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.delete( node ); + s.getTransaction().commit(); + s.close(); + } + + 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 ); + + 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(); + } + public void testAddNewChildToReadOnlyParent() throws Exception { Session s = openSession(); s.beginTransaction();