标题说明了一切.
PersistenceExceptionTranslationPostProcessor是仅用于JPA实现还是与Spring的JdbcTemplate一起使用它是否相关?
如果需要两个数据源,每个数据源都有自己的JPA实体管理器和事务管理器,那么我是否仍然只需要为整个应用程序指定一个PersistenceExceptionTranslationPostProcessor?
解决方法:
是的,可以通过在上下文中定义异常转换bean后处理器bean,将Spring异常转换机制透明地应用于使用@Repository注释的所有bean:
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
按照它doc
Bean post-processor that automatically applies persistence exception translation to any bean marked with Spring’s @Repository
annotation, adding a corresponding
PersistenceExceptionTranslationAdvisor to the exposed proxy (either an
existing AOP proxy or a newly generated proxy that implements all of
the target’s interfaces).Translates native resource exceptions to Spring’s DataAccessException hierarchy. Autodetects beans that implement the
PersistenceExceptionTranslator interface, which are subsequently asked
to translate candidate exceptions.
All of Spring’s applicable resource factories (e.g. LocalContainerEntityManagerfactorybean) implement the
PersistenceExceptionTranslator interface out of the Box. As a
consequence, all that is usually needed to enable automatic exception
translation is marking all affected beans (such as Repositories or
DAOs) with the @Repository annotation, along with defining this
post-processor as a bean in the application context.
因此,您可以将它与Jdbctemplate以及任何Jpa供应商实现一起使用
根据文档All of Spring的适用资源工厂(例如LocalContainerEntityManagerfactorybean)实现了开箱即用的PersistenceExceptionTranslator接口,我认为你仍然需要使用PersistenceExceptionTranslationPostProcessor,因为它用于转换持久化过程中生成的所有错误(HibernateExceptions,PersistenceExceptions .. .)到DataAccessException对象.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。