Author: gbadner
Date: 2010-05-10 14:10:35 -0400 (Mon, 10 May 2010)
New Revision: 19450
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/event/def/DefaultRefreshEventListener.java
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/immutable/ImmutableTest.java
Log:
HHH-5207 : Unexpected exception occurs during refresh of a detached immutable entity
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/event/def/DefaultRefreshEventListener.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/event/def/DefaultRefreshEventListener.java 2010-05-10
15:36:31 UTC (rev 19449)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/event/def/DefaultRefreshEventListener.java 2010-05-10
18:10:35 UTC (rev 19450)
@@ -152,7 +152,13 @@
// Keep the same read-only/modifiable setting for the entity that it had before
refreshing;
// If it was transient, then set it to the default for the source.
if ( result != null ) {
- source.setReadOnly( result, ( e == null ? source.isDefaultReadOnly() : e.isReadOnly()
) );
+ if ( ! persister.isMutable() ) {
+ // this is probably redundant; it should already be read-only
+ source.setReadOnly( result, true );
+ }
+ else {
+ source.setReadOnly( result, ( e == null ? source.isDefaultReadOnly() : e.isReadOnly()
) );
+ }
}
source.setFetchProfile(previousFetchProfile);
Modified:
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/immutable/ImmutableTest.java
===================================================================
---
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/immutable/ImmutableTest.java 2010-05-10
15:36:31 UTC (rev 19449)
+++
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/immutable/ImmutableTest.java 2010-05-10
18:10:35 UTC (rev 19450)
@@ -361,6 +361,84 @@
assertDeleteCount( 3 );
}
+ public void testRefreshImmutable() {
+ clearCounts();
+
+ Contract c = new Contract( null, "gavin", "phone");
+ ContractVariation cv1 = new ContractVariation(1, c);
+ cv1.setText("expensive");
+ ContractVariation cv2 = new ContractVariation(2, c);
+ cv2.setText("more expensive");
+
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ s.saveOrUpdate( c );
+ assertTrue( s.isReadOnly( c ) );
+ assertTrue( s.isReadOnly( cv1 ) );
+ assertTrue( s.isReadOnly( cv2 ) );
+ t.commit();
+ s.close();
+
+ assertInsertCount( 3 );
+ assertUpdateCount( 0 );
+ clearCounts();
+
+ s = openSession();
+ t = s.beginTransaction();
+ // refresh detached
+ s.refresh( c );
+ assertTrue( s.isReadOnly( c ) );
+ assertEquals( c.getCustomerName(), "gavin" );
+ assertEquals( c.getVariations().size(), 2 );
+ Iterator it = c.getVariations().iterator();
+ cv1 = (ContractVariation) it.next();
+ assertEquals( cv1.getText(), "expensive" );
+ cv2 = (ContractVariation) it.next();
+ assertEquals( cv2.getText(), "more expensive" );
+ assertTrue( s.isReadOnly( cv1 ) );
+ assertTrue( s.isReadOnly( cv2 ) );
+ t.commit();
+ s.close();
+
+ assertInsertCount( 0 );
+ assertUpdateCount( 0 );
+ clearCounts();
+
+ c.setCustomerName( "joe" );
+
+ s = openSession();
+ t = s.beginTransaction();
+ // refresh updated detached
+ s.refresh( c );
+ assertTrue( s.isReadOnly( c ) );
+ assertEquals( c.getCustomerName(), "gavin" );
+ assertEquals( c.getVariations().size(), 2 );
+ it = c.getVariations().iterator();
+ cv1 = (ContractVariation) it.next();
+ assertEquals( cv1.getText(), "expensive" );
+ cv2 = (ContractVariation) it.next();
+ assertEquals( cv2.getText(), "more expensive" );
+ assertTrue( s.isReadOnly( cv1 ) );
+ assertTrue( s.isReadOnly( cv2 ) );
+ t.commit();
+ s.close();
+
+ assertInsertCount( 0 );
+ assertUpdateCount( 0 );
+ clearCounts();
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.delete(c);
+ assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount()
).uniqueResult(), new Long(0) );
+ assertEquals( s.createCriteria(ContractVariation.class).setProjection(
Projections.rowCount() ).uniqueResult(), new Long(0) );
+ t.commit();
+ s.close();
+
+ assertUpdateCount( 0 );
+ assertDeleteCount( 3 );
+ }
+
public void testImmutable() {
clearCounts();