Skip to content

Commit 4428464

Browse files
committed
HHH-8593 EntityManager.refresh should throw EntityNotFoundException if the entity no longer exists in the database
1 parent bc31357 commit 4428464

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void onRefresh(RefreshEvent event, Map refreshedAlready) {
111111
LOG.tracev( "Refreshing ", MessageHelper.infoString( e.getPersister(), e.getId(), source.getFactory() ) );
112112
}
113113
if ( !e.isExistsInDatabase() ) {
114-
throw new HibernateException( "this instance does not yet exist as a row in the database" );
114+
throw new UnresolvableObjectException(e.getId(), "this instance does not yet exist as a row in the database" );
115115
}
116116

117117
persister = e.getPersister();

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,35 @@ public void testFactoryClosed() throws Exception {
445445
}
446446
}
447447

448+
@Test
449+
public void testEntityNotFoundException() throws Exception {
450+
EntityManager em = getOrCreateEntityManager();
451+
em.getTransaction().begin();
452+
Wallet w = new Wallet();
453+
w.setBrand("Lacoste");
454+
w.setModel("Minimic");
455+
w.setSerial("0324");
456+
em.persist(w);
457+
Wallet wallet = em.find( Wallet.class, w.getSerial() );
458+
em.createNativeQuery("delete from Wallet").executeUpdate();
459+
try {
460+
em.refresh(wallet);
461+
} catch (EntityNotFoundException enfe) {
462+
// success
463+
if (em.getTransaction() != null) {
464+
em.getTransaction().rollback();
465+
}
466+
em.close();
467+
return;
468+
}
469+
470+
try {
471+
em.getTransaction().commit();
472+
fail("Should have raised an EntityNotFoundException");
473+
} catch (PersistenceException pe) {
474+
} finally {
475+
em.close();
476+
}
477+
}
478+
448479
}

0 commit comments

Comments
 (0)