From 4428464d096c6a1fff3023cb707a2384ab6c0ffb Mon Sep 17 00:00:00 2001 From: Scott Marlow Date: Mon, 7 Oct 2013 13:23:24 -0400 Subject: [PATCH] HHH-8593 EntityManager.refresh should throw EntityNotFoundException if the entity no longer exists in the database --- .../internal/DefaultRefreshEventListener.java | 2 +- .../hibernate/jpa/test/EntityManagerTest.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) 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(); + } + } + }