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.dao.support;
018
019import org.springframework.dao.DataAccessException;
020import org.springframework.lang.Nullable;
021
022/**
023 * Interface implemented by Spring integrations with data access technologies
024 * that throw runtime exceptions, such as JPA and Hibernate.
025 *
026 * <p>This allows consistent usage of combined exception translation functionality,
027 * without forcing a single translator to understand every single possible type
028 * of exception.
029 *
030 * @author Rod Johnson
031 * @author Juergen Hoeller
032 * @since 2.0
033 */
034@FunctionalInterface
035public interface PersistenceExceptionTranslator {
036
037        /**
038         * Translate the given runtime exception thrown by a persistence framework to a
039         * corresponding exception from Spring's generic
040         * {@link org.springframework.dao.DataAccessException} hierarchy, if possible.
041         * <p>Do not translate exceptions that are not understood by this translator:
042         * for example, if coming from another persistence framework, or resulting
043         * from user code or otherwise unrelated to persistence.
044         * <p>Of particular importance is the correct translation to
045         * DataIntegrityViolationException, for example on constraint violation.
046         * Implementations may use Spring JDBC's sophisticated exception translation
047         * to provide further information in the event of SQLException as a root cause.
048         * @param ex a RuntimeException to translate
049         * @return the corresponding DataAccessException (or {@code null} if the
050         * exception could not be translated, as in this case it may result from
051         * user code rather than from an actual persistence problem)
052         * @see org.springframework.dao.DataIntegrityViolationException
053         * @see org.springframework.jdbc.support.SQLExceptionTranslator
054         */
055        @Nullable
056        DataAccessException translateExceptionIfPossible(RuntimeException ex);
057
058}