more consistent error message between JPA and plain Hibernate

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-09-01 16:31:14 +02:00
parent d7aa5f7a78
commit f95182e736
3 changed files with 6 additions and 5 deletions

View File

@ -48,7 +48,6 @@ import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer; import org.hibernate.proxy.LazyInitializer;
import org.hibernate.type.CollectionType; import org.hibernate.type.CollectionType;
import org.hibernate.type.ComponentType; import org.hibernate.type.ComponentType;
import org.hibernate.type.CompositeType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.hibernate.type.TypeHelper; import org.hibernate.type.TypeHelper;
@ -315,7 +314,7 @@ public class DefaultDeleteEventListener implements DeleteEventListener, Callback
EntityPersister persister = source.getEntityPersister( entityName, event.getObject() ); EntityPersister persister = source.getEntityPersister( entityName, event.getObject() );
Object id = persister.getIdentifier( event.getObject(), source ); Object id = persister.getIdentifier( event.getObject(), source );
entityName = entityName == null ? source.guessEntityName( event.getObject() ) : entityName; 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 + "]" );
} }
/** /**

View File

@ -21,6 +21,7 @@ import java.util.function.Consumer;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.hibernate.Internal; import org.hibernate.Internal;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.SessionFactoryObserver; import org.hibernate.SessionFactoryObserver;
import org.hibernate.boot.CacheRegionDefinition; import org.hibernate.boot.CacheRegionDefinition;
@ -185,8 +186,9 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
*/ */
public static final JpaEntityNotFoundDelegate INSTANCE = new JpaEntityNotFoundDelegate(); public static final JpaEntityNotFoundDelegate INSTANCE = new JpaEntityNotFoundDelegate();
public void handleEntityNotFound(String entityName, Object id) { public void handleEntityNotFound(String entityName, Object identifier) {
throw new EntityNotFoundException( "Unable to find " + entityName + " with id " + id ); final ObjectNotFoundException exception = new ObjectNotFoundException( entityName, identifier );
throw new EntityNotFoundException( exception.getMessage(), exception);
} }
} }

View File

@ -318,7 +318,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
() -> Hibernate.initialize( account.getParentAccount() ) () -> Hibernate.initialize( account.getParentAccount() )
); );
// Account with id 1 does not exist // Account with id 1 does not exist
assertTrue( exception.getMessage().endsWith( " 1" ) ); assertTrue( exception.getMessage().endsWith( "#1]" ) );
} ); } );
} }