提交 ddb41172 编写于 作者: J Juergen Hoeller

HibernateExceptionTranslator converts JPA exceptions as well (for Hibernate 5.2)

Issue: SPR-14455
上级 adfea826
......@@ -16,14 +16,18 @@
package org.springframework.orm.hibernate5;
import javax.persistence.PersistenceException;
import org.hibernate.HibernateException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
/**
* {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
* instances to Spring's {@link DataAccessException} hierarchy.
* instances to Spring's {@link DataAccessException} hierarchy. As of Spring 4.3.2 and
* Hibernate 5.2, it also converts standard JPA {@link PersistenceException} instances.
*
* <p>Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
* translator in addition to a {@code LocalSessionFactoryBean}.
......@@ -35,6 +39,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
* @since 4.2
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
* @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
* @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible(RuntimeException)
*/
public class HibernateExceptionTranslator implements PersistenceExceptionTranslator {
......@@ -43,7 +48,10 @@ public class HibernateExceptionTranslator implements PersistenceExceptionTransla
if (ex instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex);
}
return null;
if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex.getCause());
}
return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册