diff --git a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java index 5e4886f2f8f..38070d625a0 100644 --- a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java +++ b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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. * *

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,13 +48,16 @@ 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); } /** * Convert the given HibernateException to an appropriate exception from the * {@code org.springframework.dao} hierarchy. - * @param ex HibernateException that occured + * @param ex HibernateException that occurred * @return a corresponding DataAccessException * @see SessionFactoryUtils#convertHibernateAccessException */