Class PersistenceExceptionTranslationInterceptor

java.lang.Object
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.InitializingBean

public class PersistenceExceptionTranslationInterceptor extends Object implements org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.InitializingBean
AOP Alliance MethodInterceptor that provides persistence exception translation based on a given PersistenceExceptionTranslator.

Delegates to the given PersistenceExceptionTranslator to translate a RuntimeException thrown into Spring's DataAccessException hierarchy (if appropriate). If the RuntimeException in question is declared on the target method, it is always propagated as-is (with no translation applied).

Since:
2.0
Author:
Rod Johnson, Juergen Hoeller
See Also:
  • Constructor Details

    • PersistenceExceptionTranslationInterceptor

      public PersistenceExceptionTranslationInterceptor()
      Create a new PersistenceExceptionTranslationInterceptor. Needs to be configured with a PersistenceExceptionTranslator afterwards.
      See Also:
    • PersistenceExceptionTranslationInterceptor

      public PersistenceExceptionTranslationInterceptor(PersistenceExceptionTranslator pet)
      Create a new PersistenceExceptionTranslationInterceptor for the given PersistenceExceptionTranslator.
      Parameters:
      pet - the PersistenceExceptionTranslator to use
    • PersistenceExceptionTranslationInterceptor

      public PersistenceExceptionTranslationInterceptor(org.springframework.beans.factory.ListableBeanFactory beanFactory)
      Create a new PersistenceExceptionTranslationInterceptor, autodetecting PersistenceExceptionTranslators in the given BeanFactory.
      Parameters:
      beanFactory - the ListableBeanFactory to obtaining all PersistenceExceptionTranslators from
  • Method Details

    • setPersistenceExceptionTranslator

      public void setPersistenceExceptionTranslator(PersistenceExceptionTranslator pet)
      Specify the PersistenceExceptionTranslator to use.

      Default is to autodetect all PersistenceExceptionTranslators in the containing BeanFactory, using them in a chain.

      See Also:
    • setAlwaysTranslate

      public void setAlwaysTranslate(boolean alwaysTranslate)
      Specify whether to always translate the exception ("true"), or whether throw the raw exception when declared, i.e. when the originating method signature's exception declarations allow for the raw exception to be thrown ("false").

      Default is "false". Switch this flag to "true" in order to always translate applicable exceptions, independent of the originating method signature.

      Note that the originating method does not have to declare the specific exception. Any base class will do as well, even throws Exception: As long as the originating method does explicitly declare compatible exceptions, the raw exception will be rethrown. If you would like to avoid throwing raw exceptions in any case, switch this flag to "true".

    • setBeanFactory

      public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
      Throws:
      org.springframework.beans.BeansException
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • invoke

      @Nullable public Object invoke(org.aopalliance.intercept.MethodInvocation mi) throws Throwable
      Specified by:
      invoke in interface org.aopalliance.intercept.MethodInterceptor
      Throws:
      Throwable
    • detectPersistenceExceptionTranslators

      protected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(org.springframework.beans.factory.ListableBeanFactory bf)
      Detect all PersistenceExceptionTranslators in the given BeanFactory.
      Parameters:
      bf - the ListableBeanFactory to obtain PersistenceExceptionTranslators from
      Returns:
      a chained PersistenceExceptionTranslator, combining all PersistenceExceptionTranslators found in the given bean factory
      See Also: