diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java index 2c4de4a1fd..bf8e4344d7 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java @@ -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(); diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java index 89360d4a47..e4fba67edc 100755 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java @@ -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(); + } + } + }