无法找到Spring STS javax / persistence / EntityManagerFactory

时间:2016-02-25 08:25:27

标签: java spring spring-mvc spring-tool-suite

我希望将我的Spring项目连接到带有jpa的数据库

我添加了此代码。 jsp.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="org.baeldung.persistence.model" />
  <property name="jpaVendorAdapter">
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
  </property>
  <property name="jpaProperties">
     <props>
        <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
     </props>
  </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="mysql://kimdg3550.cafe24.com/kimdg3550" />
   <property name="username" value="secret" />
   <property name="password" value="secret" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="myEmf" />
</bean>
 <tx:annotation-driven />

<bean id="persistenceExceptionTranslationPostProcessor"
  class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

但是我收到了这个错误

enter image description here

  

构建路径不完整。找不到javax / persistence / EntityManagerFactory的类文件jsp.xml / spring / src / main / webapp / WEB-INF / spring / appServlet第32行Spring AOP问题

如何避免此错误?

2 个答案:

答案 0 :(得分:1)

要解决此问题,您必须将一个JPA实现添加到类路径中。 Hibernate是JPA标准的主流实现之一。

答案 1 :(得分:0)

我通过将此代码添加到pom.xml

来解决了这个错误
<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
</dependency>