HHH-13300 Correctly convert Hibernate exceptions to JPA in executeUpdate()

This commit is contained in:
Gail Badner 2019-03-19 23:42:51 -07:00 committed by Yoann Rodière
parent 5a1efe883e
commit 634782fef6
2 changed files with 10 additions and 9 deletions

View File

@ -1614,12 +1614,7 @@ public abstract class AbstractProducedQuery<R> implements QueryImplementor<R> {
throw new IllegalArgumentException( e );
}
catch ( HibernateException e) {
if ( getProducer().getFactory().getSessionFactoryOptions().isJpaBootstrap() ) {
throw getExceptionConverter().convert( e );
}
else {
throw e;
}
throw getExceptionConverter().convert( e );
}
finally {
afterQuery();

View File

@ -12,6 +12,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PersistenceException;
import org.hibernate.HibernateException;
import org.hibernate.annotations.Immutable;
@ -24,6 +25,7 @@ import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
@ -61,10 +63,14 @@ public class ImmutableEntityUpdateQueryHandlingModeExceptionTest extends BaseNon
.setParameter( "name", "N/A" )
.executeUpdate();
} );
fail("Should throw HibernateException");
fail("Should throw PersistenceException");
}
catch (HibernateException e) {
assertEquals( "The query: [update Country set name = :name] attempts to update an immutable entity: [Country]", e.getMessage() );
catch (PersistenceException e) {
assertTrue( e.getCause() instanceof HibernateException );
assertEquals(
"The query: [update Country set name = :name] attempts to update an immutable entity: [Country]",
e.getCause().getMessage()
);
}
doInHibernate( this::sessionFactory, session -> {