From d234b4eea93a699980c33bc7dae42dcf7e08de5e Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 5 Jan 2024 10:32:07 +0100 Subject: [PATCH] HHH-14821 consistent use of exception types (and exception message formats) --- .../event/internal/DefaultRefreshEventListener.java | 11 ++++------- .../org/hibernate/orm/test/refresh/RefreshTest.java | 8 +++----- 2 files changed, 7 insertions(+), 12 deletions(-) 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 b4025f31c2..9aa230a4c0 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 @@ -9,7 +9,8 @@ package org.hibernate.event.internal; import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.LockOptions; -import org.hibernate.PersistentObjectException; +import org.hibernate.NonUniqueObjectException; +import org.hibernate.TransientObjectException; import org.hibernate.UnresolvableObjectException; import org.hibernate.cache.spi.access.CollectionDataAccess; import org.hibernate.cache.spi.access.EntityDataAccess; @@ -128,8 +129,7 @@ public class DefaultRefreshEventListener implements RefreshEventListener { persister = source.getEntityPersister( event.getEntityName(), object ); id = persister.getIdentifier( object, event.getSession() ); if ( id == null ) { - throw new HibernateException( "attempted to refresh an instance that is not part of the persistence context yet: " - + infoString( persister, id, source.getFactory() )); + throw new TransientObjectException( "transient instance passed to refresh"); } if ( LOG.isTraceEnabled() ) { LOG.tracev( @@ -138,10 +138,7 @@ public class DefaultRefreshEventListener implements RefreshEventListener { ); } if ( persistenceContext.getEntry( source.generateEntityKey( id, persister ) ) != null ) { - throw new PersistentObjectException( - "attempted to refresh transient instance when persistent instance was already associated with the Session: " - + infoString( persister, id, source.getFactory() ) - ); + throw new NonUniqueObjectException( id, persister.getEntityName() ); } } else { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshTest.java index 0dd05697db..120d1197c2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/refresh/RefreshTest.java @@ -6,8 +6,7 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.Set; -import org.hibernate.HibernateException; - +import org.hibernate.TransientObjectException; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; @@ -81,7 +80,7 @@ public class RefreshTest { @Test public void testRefreshWithNullId(SessionFactoryScope scope) { Assertions.assertThrows( - HibernateException.class, + TransientObjectException.class, () -> { scope.inTransaction( session -> { @@ -90,8 +89,7 @@ public class RefreshTest { session.refresh( se ); } ); - }, - "attempted to refresh an instance that is not part of the persistence context yet: [org.hibernate.orm.test.refresh.RefreshTest$SimpleEntity#]" + } ); }