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

(cherry picked from commit 634782fef6)
This commit is contained in:
Gail Badner 2019-03-19 23:42:51 -07:00 committed by gbadner
parent a93a5183ba
commit bf85bfcf99
2 changed files with 11 additions and 9 deletions

View File

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

View File

@ -8,6 +8,8 @@ package org.hibernate.test.annotations.immutable;
import java.util.Map; import java.util.Map;
import javax.persistence.PersistenceException;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
@ -18,6 +20,7 @@ import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -54,10 +57,14 @@ public class ImmutableEntityUpdateQueryHandlingModeExceptionTest extends BaseNon
.setParameter( "name", "N/A" ) .setParameter( "name", "N/A" )
.executeUpdate(); .executeUpdate();
} ); } );
fail("Should throw HibernateException"); fail("Should throw PersistenceException");
} }
catch (HibernateException e) { catch (PersistenceException e) {
assertEquals( "The query: [update Country set name = :name] attempts to update an immutable entity: [Country]", e.getMessage() ); 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 -> { doInHibernate( this::sessionFactory, session -> {