HHH-14821 consistent use of exception types

(and exception message formats)
This commit is contained in:
Gavin King 2024-01-05 10:32:07 +01:00
parent 942e5cdda4
commit 63a84da40e
2 changed files with 7 additions and 12 deletions

View File

@ -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;
@ -93,8 +94,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(
@ -103,10 +103,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 {

View File

@ -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#<null>]"
}
);
}