在 Bean 工厂中没有发现持久性异常转换器。无法执行异常转换

2022-09-02 02:50:00

迁移到带有Hibernate 4.0.0 CR7的Spring Framework 3.1.0 RC1后,我遇到了以下异常

2011-12-08/11:48:38.425/EST [Thread-17] 错误 上下文初始化失败 org.springframework.beans.factory.BeanCreationException: 创建 bean 时出错,名称为 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' 在 ServletContext 资源 [/WEB-INF/applicationContext-hibernate.xml]中定义:Bean 初始化失败;嵌套异常是 java.lang.IllegalStateException:在 bean 工厂中找不到持久性异常转换器。无法执行异常转换。at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:530) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:728) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449) at org.springframework.web.context.contextloader.configureAndRefreshWebApplicationContext(ContextLoader.java:381) at org.springframework.web.context.contextloader.initWebApplicationContext(ContextLoader.java:283) at org.springframework.web.context.contextloaderListener.contextInitialized(ContextLoaderListener.java:111) atorg.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadExecutor.runWorker(Unknown Source) atjava.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 由:java.lang.IllegalStateException:在 bean factory 中找不到持久性异常转换器。无法执行异常转换。at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTransceptor.java:142) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.(PersistenceExceptionTranslationInterceptor.java:79) at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.(PersistenceExceptionTranslationAdvisor.java:70) at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:103) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1465) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1433) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) ...另外 18


答案 1

使用新的弹簧java配置:

    @Bean 
    public HibernateExceptionTranslator hibernateExceptionTranslator(){ 
      return new HibernateExceptionTranslator(); 
    }

答案 2

正如 skravin 所提到的,您必须配置一个 HibernateExceptionTranslator。For Hibernate 3 实现了,但 Hibernate 4 的那个没有实现。LocalSessionFactoryBeanPersistenceExceptionTranslator


推荐