001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.orm.hibernate5;
018
019import javax.persistence.PersistenceException;
020
021import org.hibernate.HibernateException;
022
023import org.springframework.dao.DataAccessException;
024import org.springframework.dao.support.PersistenceExceptionTranslator;
025import org.springframework.orm.jpa.EntityManagerFactoryUtils;
026
027/**
028 * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
029 * instances to Spring's {@link DataAccessException} hierarchy. As of Spring 4.3.2 and
030 * Hibernate 5.2, it also converts standard JPA {@link PersistenceException} instances.
031 *
032 * <p>Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
033 * translator in addition to a {@code LocalSessionFactoryBean}.
034 *
035 * <p>When configuring the container with {@code @Configuration} classes, a {@code @Bean}
036 * of this type must be registered manually.
037 *
038 * @author Juergen Hoeller
039 * @since 4.2
040 * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
041 * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
042 * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible(RuntimeException)
043 */
044public class HibernateExceptionTranslator implements PersistenceExceptionTranslator {
045
046        @Override
047        public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
048                if (ex instanceof HibernateException) {
049                        return convertHibernateAccessException((HibernateException) ex);
050                }
051                if (ex instanceof PersistenceException) {
052                        if (ex.getCause() instanceof HibernateException) {
053                                return convertHibernateAccessException((HibernateException) ex.getCause());
054                        }
055                        return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
056                }
057                return null;
058        }
059
060        /**
061         * Convert the given HibernateException to an appropriate exception from the
062         * {@code org.springframework.dao} hierarchy.
063         * @param ex HibernateException that occurred
064         * @return a corresponding DataAccessException
065         * @see SessionFactoryUtils#convertHibernateAccessException
066         */
067        protected DataAccessException convertHibernateAccessException(HibernateException ex) {
068                return SessionFactoryUtils.convertHibernateAccessException(ex);
069        }
070
071}