NoClassDefFoundError: javax/persistence/Converter when importing jpa application context in webservice module

NoClassDefFoundError: javax/persistence/Converter when importing jpa application context in webservice module

在我的多模块 maven 项目中从我的 webservice 模块调用 jpa 模块时出现以下错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/applicationContext-mysql.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/persistence/Converter

我在 webservice 模块的 spring-ws-servlet.xml 文件中添加了资源导入

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:sws="http://www.springframework.org/schema/web-services" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="org.learn.spring" />

  <sws:annotation-driven/>

  <sws:dynamic-wsdl id="osinvoke" portTypeName="EAIOSInvoke" locationUri="/osinvoke/" targetNamespace="http://spring.learn.com/eai/definitions">
    <sws:xsd location="/WEB-INF/osinvoke.xsd" />
  </sws:dynamic-wsdl>

  <import resource="classpath:/spring/applicationContext-mysql.xml" />
</beans>

这里是来自 jpa 模块的 applicationContext-mysql.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

  <tx:annotation-driven />
  <context:annotation-config />
  <context:component-scan base-package="org.learn.spring" />

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/cmddb" />
    <property name="username" value="testdb" />
    <property name="password" value="testpass" />
  </bean>

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="jpaData" />
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">false</prop>
        <prop key="hibernate.hbm2ddl.auto">validate</prop>
      </props>
    </property>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>

</beans>

jpa 模块中的单元测试能够检测应用程序上下文并从上下文文件创建实体管理器。 Web 服务模块无法创建 entityManagerFactory 对象。

您的 JPA 提供程序需要 JPA 2.1,而您的 CLASSPATH 中没有那个 jar(相反,您有一个更早的 JPA API jar)。这类问题已经被问过很多次了

从 TomEE 切换到 Tomcat 修复了类路径错误。进一步研究类路径问题,在 TomEE 中有一个 maven plugin to help with configuration of jars。感谢您的帮助。

tomcat 库应该具有与我们添加为 Maven 依赖项相同的 JPA 版本。我被这个问题困了一天才弄明白。