JPA和Spring MVC配置错误? [英] JPA and Spring MVC wrong configuration?

查看:137
本文介绍了JPA和Spring MVC配置错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行测试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" />

问题出在哪里?

解决方案

是否需要在<persistence-unit>下添加<provider>标签?

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

另外,此答案建议,在/WEB-INF/classes/META-INF中没有persistence.xml也会触发此错误. /p>

I get this exception when I try to run my test web application:

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

My persistence.xml file is in META-INF folder:

<?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>

My data-context.xml bean definiton file is into WEB-INF folder:

<?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" />

Where is the problem?

解决方案

Do you need to add a <provider> tag under <persistence-unit>?

(e.g. <provider>org.hibernate.ejb.HibernatePersistence</provider>)

Also, this answer suggested that not having your persistence.xml in /WEB-INF/classes/META-INF might also trigger this error.

这篇关于JPA和Spring MVC配置错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆