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