HHH-4880 : EntityManager.refresh does not throw EntityNotFoundException for removed entity

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18700 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2010-02-05 06:25:59 +00:00
parent 8606c4a097
commit c18a9306b9
2 changed files with 85 additions and 1 deletions

View File

@ -151,7 +151,9 @@ public class DefaultRefreshEventListener implements RefreshEventListener {
Object result = persister.load( id, object, event.getLockOptions(), source ); Object result = persister.load( id, object, event.getLockOptions(), source );
// Keep the same read-only/modifiable setting for the entity that it had before refreshing; // 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 it was transient, then set it to the default for the source.
source.setReadOnly( result, ( e == null ? source.isDefaultReadOnly() : e.isReadOnly() ) ); if ( result != null ) {
source.setReadOnly( result, ( e == null ? source.isDefaultReadOnly() : e.isReadOnly() ) );
}
source.setFetchProfile(previousFetchProfile); source.setFetchProfile(previousFetchProfile);
UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() ); UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );

View File

@ -38,6 +38,7 @@ import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.TransientObjectException; import org.hibernate.TransientObjectException;
import org.hibernate.SessionException; import org.hibernate.SessionException;
import org.hibernate.UnresolvableObjectException;
import org.hibernate.engine.SessionImplementor; import org.hibernate.engine.SessionImplementor;
import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer; import org.hibernate.proxy.LazyInitializer;
@ -882,7 +883,88 @@ public class ReadOnlyProxyTest extends FunctionalTestCase {
s.delete( dp ); s.delete( dp );
t.commit(); t.commit();
s.close(); s.close();
}
public void testReadOnlyRefreshDeleted() {
Session s = openSession();
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);
t.commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
t = s.beginTransaction();
HibernateProxy dpProxy = ( HibernateProxy ) s.load( DataPoint.class, dp.getId() );
assertFalse( Hibernate.isInitialized( dpProxy ) );
t.commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
t = s.beginTransaction();
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 ) {
// expected
}
finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
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();
t = s.beginTransaction();
assertTrue( dpProxyInit instanceof HibernateProxy );
assertTrue( Hibernate.isInitialized( dpProxyInit ) );
try {
s.refresh( dpProxyInit );
fail( "should have thrown UnresolvableObjectException" );
}
catch ( UnresolvableObjectException ex ) {
// expected
}
finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
assertTrue( dpProxy instanceof HibernateProxy );
try {
s.refresh( dpProxy );
assertFalse( Hibernate.isInitialized( dpProxy ) );
Hibernate.initialize( dpProxy );
fail( "should have thrown UnresolvableObjectException" );
}
catch ( UnresolvableObjectException ex ) {
// expected
}
finally {
t.rollback();
s.close();
}
} }
public void testReadOnlyRefreshDetached() { public void testReadOnlyRefreshDetached() {