将spring-orm jar升级到4.1.6后,org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean失败

时间:2016-04-05 14:54:33

标签: spring-4 hibernate3 jpa-1.0

最初在我们的项目中,我们使用的是spring 3 + hibernate 3 最近已经决定升级第三方罐子,因为那个春天的一部分升级到4但不是休眠(仍然维持休眠3)。 现在我们没有更改Spring 4升级的任何代码,当我们执行项目LocalContainerEntityManagerFactoryBean注入失败时。我已经给出了上下文文件声明&下面的错误堆栈跟踪。现在的问题是如何在没有升级休眠的情况下在春季4上使现有项目工作。

使用Spring ORM 4.1.6 LocalContainerEntityManagerFactoryBean正在尝试加载JPA 2.0特定的hibernate导入(即hibernate 4 jar类)并失败。

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    <property name="persistenceUnitName" value="casp-portal"/>
</bean>
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
    <!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

错误堆栈跟踪

  

引起:org.springframework.beans.factory.BeanCreationException:   创建名称为&#39; entityManagerFactory&#39;的bean时出错在课堂上定义   路径资源[applicationContext-securityEJB-DBtest.xml]:   bean类型的后处理失败[class   org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]   失败;嵌套异常是java.lang.IllegalStateException:失败   内省bean类   [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]   对于持久性元数据:无法找到它所依赖的类   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:936)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)     在   org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:93)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)     在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)     在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)...   11更多引起:java.lang.IllegalStateException:失败   内省bean类   [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]   对于持久性元数据:无法找到它所依赖的类   org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:396)     在   org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:333)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:931)     ... 44更多引起:java.lang.NoClassDefFoundError:   javax / persistence / ValidationMode at   java.lang.Class.getDeclaredMethods0(Native Method)at   java.lang.Class.privateGetDeclaredMethods(Class.java:2615)at at   java.lang.Class.getDeclaredMethods(Class.java:1860)at   org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:420)     在   org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:392)     ... 46更多引起:java.lang.ClassNotFoundException:   javax.persistence.ValidationMode at   java.net.URLClassLoader $ 1.run(URLClassLoader.java:366)at   java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)at   java.security.AccessController.doPrivileged(Native Method)at   java.net.URLClassLoader.findClass(URLClassLoader.java:354)at   java.lang.ClassLoader.loadClass(ClassLoader.java:425)at   sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)at at   java.lang.ClassLoader.loadClass(ClassLoader.java:358)... 51更多

1 个答案:

答案 0 :(得分:0)

目前项目中使用的技术 :Hibernate 3 +最近的技术堆栈升级Spring已更改为4.1版本。 Spring 4.1强制使用JPA 2.0,同时它支持后向支持,这意味着它支持JPA 1.0,参见下面的Spring网站描述。

  

Java EE 6或更高版本现在被认为是Spring的基准   框架4,具有JPA 2.0和Servlet 3.0规范   特别相关。为了与Google App保持兼容   引擎和较旧的应用程序服务器,可以部署一个   Spring 4应用程序进入Servlet 2.5环境。但是,Servlet   强烈建议使用3.0+,这是Spring测试的先决条件和开发环境中测试设置的模拟包。

实际问题 :在下面突出显示的bean注入时间内,jUnit测试用例失败。扩展Spring-ORM类的LocalContainerEntityManagerFactoryBean试图加载JPA 2.0特定的hibernate导入并失败。

<bean id="entityManagerFactory"
    class="com.abc.persist.LocalContainerEntityManagerFactoryBean">

    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence-security.xml"/>
    <property name="persistenceUnitName" value="casp-portal"/>
    <property name="jpaPropertyMap">
        <map>
          <entry key="javax.persistence.sharedCache.mode" value="none" />
        </map>
      </property>
</bean>
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
    <!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

解决方案

  1. 选项I :保留旧版本的Spring-ORM以使其正常工作[这就是我们目前正在进行的解决方案]。

  2. 选项II :将3,4个spring-orm java类复制到我们的项目中,并根据我们的需要调整spring类代码[我打算以这种方式实现]。

  3. 选项III :这是在研究中,试图寻找另一种注入entityManager的替代方法。