0

当我尝试运行我的测试 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.xmlbean 定义文件在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" />

哪里有问题?

4

1 回答 1

1

你需要在<provider>下面添加标签<persistence-unit>吗?

(例如<provider>org.hibernate.ejb.HibernatePersistence</provider>

此外,此答案表明没有您的persistence.xml参与/WEB-INF/classes/META-INF也可能会触发此错误。

于 2012-04-11T16:58:17.507 回答