From f95182e736f2bb1ef3cf4d02e8615a513b48a59c Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sun, 1 Sep 2024 16:31:14 +0200 Subject: [PATCH] more consistent error message between JPA and plain Hibernate Signed-off-by: Gavin King --- .../event/internal/DefaultDeleteEventListener.java | 3 +-- .../jpa/boot/internal/EntityManagerFactoryBuilderImpl.java | 6 ++++-- .../src/test/java/org/hibernate/orm/test/pc/FilterTest.java | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java index 81a5c980d7..8d4db1683a 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java @@ -48,7 +48,6 @@ import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.LazyInitializer; import org.hibernate.type.CollectionType; import org.hibernate.type.ComponentType; -import org.hibernate.type.CompositeType; import org.hibernate.type.Type; import org.hibernate.type.TypeHelper; @@ -315,7 +314,7 @@ public class DefaultDeleteEventListener implements DeleteEventListener, Callback EntityPersister persister = source.getEntityPersister( entityName, event.getObject() ); Object id = persister.getIdentifier( event.getObject(), source ); entityName = entityName == null ? source.guessEntityName( event.getObject() ) : entityName; - throw new IllegalArgumentException( "Removing a detached instance " + entityName + "#" + id ); + throw new IllegalArgumentException( "Given entity is not associated with the persistence context [" + entityName + "#" + id + "]" ); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 5cd86951b8..14d834d51a 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -21,6 +21,7 @@ import java.util.function.Consumer; import javax.sql.DataSource; import org.hibernate.Internal; +import org.hibernate.ObjectNotFoundException; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.boot.CacheRegionDefinition; @@ -185,8 +186,9 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil */ public static final JpaEntityNotFoundDelegate INSTANCE = new JpaEntityNotFoundDelegate(); - public void handleEntityNotFound(String entityName, Object id) { - throw new EntityNotFoundException( "Unable to find " + entityName + " with id " + id ); + public void handleEntityNotFound(String entityName, Object identifier) { + final ObjectNotFoundException exception = new ObjectNotFoundException( entityName, identifier ); + throw new EntityNotFoundException( exception.getMessage(), exception); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/pc/FilterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/pc/FilterTest.java index e14f878ab8..ae14cf301d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/pc/FilterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/pc/FilterTest.java @@ -318,7 +318,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase { () -> Hibernate.initialize( account.getParentAccount() ) ); // Account with id 1 does not exist - assertTrue( exception.getMessage().endsWith( " 1" ) ); + assertTrue( exception.getMessage().endsWith( "#1]" ) ); } ); }