问题
I'm getting the following error when calling the jpa module from my webservice module in my multi-module maven project.
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
I've added a resource import in my spring-ws-servlet.xml file in the webservice module
<?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>
And here is the applicationContext-mysql.xml file from the jpa module
<?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>
The unit tests in jpa module are able to detect the application context and create the entity manager from the context file. The webservice module is unable to create the entityManagerFactory object.
回答1:
Switching from TomEE to Tomcat fixed the classpath errors. Researching the classpath issue further, there is a maven plugin to help with configuration of jars in TomEE. Thank you for your help.
回答2:
Your JPA provider requires JPA 2.1, and you don't have that jar in your CLASSPATH (instead you have an earlier JPA API jar). This sort of question has been asked many times on here
来源:https://stackoverflow.com/questions/27748998/noclassdeffounderror-javax-persistence-converter-when-importing-jpa-application