【问题标题】:JPA and Spring MVC wrong configuration?JPA 和 Spring MVC 配置错误?
【发布时间】:2012-04-11 16:54:12
【问题描述】:

当我尝试运行我的测试 Web 应用程序时出现此异常:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myDAO': 
Injection of persistence dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'emf' defined in ServletContext resource 
[/WEB-INF/testapp-servlet.xml]:
Invocation of init method failed; 
nested exception is javax.persistence.PersistenceException: 
No Persistence provider for EntityManager named testapp

我的persistence.xml 文件在META-INF 文件夹中:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="testapp" transaction-type="RESOURCE_LOCAL">
    <class>it.testapp.entities.Person</class>
    <class>it.testapp.entities.Group</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="javax.persistence.jdbc.url"
            value="jdbc:sqlserver://localhost;databaseName=db_testapp" />
        <property name="javax.persistence.jdbc.user" value="user1" />
        <property name="javax.persistence.jdbc.password" value="" />
    </properties>

</persistence-unit>

我的data-context.xml bean 定义文件位于WEB-INF 文件夹中:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<context:annotation-config />
<context:component-scan base-package="it.testapp.dao" />

<bean id="emf"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="testapp"></property>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="aDao" class="it.testapp.dao.jpa.PersonJpaDAO" />

问题出在哪里?

【问题讨论】:

标签: java spring jpa spring-mvc


【解决方案1】:

需要在&lt;persistence-unit&gt;下添加&lt;provider&gt;标签吗?

(例如&lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;

另外,this answer 建议在/WEB-INF/classes/META-INF 中没有您的persistence.xml 也可能会触发此错误。

【讨论】:

  • 这不是一个不同的例外或什么?如果没有,org.hibernate.ejb.HibernatePersistence 在你的类路径中吗?
  • 同样的异常...我使用的是 Hibernate 4,所以在我的类路径中有 hibernate-entitymanager-4.1.1.Final.jar
  • 抱歉,问题是我的persistence.xml/WebContent/META_INF(我使用的是Eclipse)而不是/src/META-INF。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2017-06-24
  • 2017-02-10
  • 2016-06-12
  • 2019-03-01
  • 2014-10-08
  • 2014-02-05
  • 2020-09-29
  • 2017-03-19
相关资源
最近更新 更多