HHH-8593 EntityManager.refresh should throw EntityNotFoundException if the entity no longer exists in the database

This commit is contained in:
Scott Marlow 2013-10-07 13:23:24 -04:00
parent bc31357436
commit 4428464d09
2 changed files with 32 additions and 1 deletions

View File

@ -111,7 +111,7 @@ public class DefaultRefreshEventListener implements RefreshEventListener {
LOG.tracev( "Refreshing ", MessageHelper.infoString( e.getPersister(), e.getId(), source.getFactory() ) );
}
if ( !e.isExistsInDatabase() ) {
throw new HibernateException( "this instance does not yet exist as a row in the database" );
throw new UnresolvableObjectException(e.getId(), "this instance does not yet exist as a row in the database" );
}
persister = e.getPersister();

View File

@ -445,4 +445,35 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
}
}
@Test
public void testEntityNotFoundException() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Wallet w = new Wallet();
w.setBrand("Lacoste");
w.setModel("Minimic");
w.setSerial("0324");
em.persist(w);
Wallet wallet = em.find( Wallet.class, w.getSerial() );
em.createNativeQuery("delete from Wallet").executeUpdate();
try {
em.refresh(wallet);
} catch (EntityNotFoundException enfe) {
// success
if (em.getTransaction() != null) {
em.getTransaction().rollback();
}
em.close();
return;
}
try {
em.getTransaction().commit();
fail("Should have raised an EntityNotFoundException");
} catch (PersistenceException pe) {
} finally {
em.close();
}
}
}