spring - 没有命名 EntityManager 的持久性提供程序

标签 spring hibernate jpa persistence persistence.xml

我收到错误“名为 projectPersistence 的 EntityManager 没有持久性提供程序
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
"当我在服务器上运行我的项目时。这是我的配置文件:

持久化文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence:persistence version="1.0"
    xmlns:persistence="http://java.sun.com/xml/ns/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_1_0.xsd ">
    <persistence:persistence-unit name="projectPersistence"
        transaction-type="RESOURCE_LOCAL">
        <persistence:provider>org.hibernate.ejb.HibernatePersistence</persistence:provider>
        <persistence:mapping-file>orm.xml</persistence:mapping-file>
        <persistence:class>it.test.home.entity.User</persistence:class>
        <persistence:properties>
            <persistence:property name="hibernate.connection.driver_class"
                value="com.mysql.jdbc.Driver" />
            <persistence:property name="hibernate.connection.password"
                value="root" />
            <persistence:property name="hibernate.connection.url"
                value="jdbc:mysql://localhost:3306/hibernate_db" />
            <persistence:property name="hibernate.connection.username"
                value="root" />
            <persistence:property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQLDialect" />

        </persistence:properties>

    </persistence:persistence-unit>

</persistence:persistence>

UserDaoImpl.java
package it.test.home.dao;
// Generated 4-mar-2012 19.36.54 by Hibernate Tools 3.4.0.CR1


import it.test.home.entity.User;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Home object for domain model class UserTable.
 * @see it.test.home.entity.User
 * @author Hibernate Tools
 */
@Stateless
public class UserDaoImpl implements UserDao {

    private static final Log log = LogFactory.getLog(UserDaoImpl.class);

    @PersistenceContext(unitName="projectPersistence")
    private EntityManager entityManager;

    @Override
    public void persist(User transientInstance) {
        log.debug("persisting UserTable instance");
        try {
            entityManager.persist(transientInstance);
            log.debug("persist successful");
        }
        catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    @Override
    public void remove(User persistentInstance) {
        log.debug("removing UserTable instance");
        try {
            entityManager.remove(persistentInstance);
            log.debug("remove successful");
        }
        catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    @Override
    public User merge(User detachedInstance) {
        log.debug("merging UserTable instance");
        try {
            User result = entityManager.merge(detachedInstance);
            log.debug("merge successful");
            return result;
        }
        catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    @Override
    public User findById( String id) {
        log.debug("getting UserTable instance with id: " + id);
        try {
            User instance = entityManager.find(User.class, id);
            log.debug("get successful");
            return instance;
        }
        catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
}

projectPersistence-dao.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

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

    <bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="persistenceUnitName" value="projectPersistence" />
    </bean>
    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean autowire="byName" class="it.test.home.dao.UserDaoImpl" id="userDao">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <context:component-scan base-package="it.test.home.entity" />
    <tx:annotation-driven />



</beans>

projectPersistence-enterprise.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <import resource="projectPersistence-dao.xml" />
    <context:annotation-config />
    <tx:annotation-driven />


</beans>

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

    <bean
        class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg value="classpath*:projectPersistence-enterprise.xml" />
    </bean>
</beans>

servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:import resource="classpath*:beanRefContext.xml"/>

    <context:component-scan base-package="it.test.home" />



</beans:beans>

和,UserServiceImpl.java
package it.test.home.service;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;
import org.springframework.stereotype.Service;

import it.test.home.dao.UserDao;
import it.test.home.entity.User;

@Service("userService")
@Stateless(mappedName="UserService", name="UserService")
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void persist(User transientInstance) {
        userDao.persist(transientInstance);

    }

    @Override
    public void remove(User persistentInstance) {
        userDao.remove(persistentInstance);

    }

    @Override
    public User merge(User detachedInstance) {
        return userDao.merge(detachedInstance);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

}

这是我的控制台的日志:
ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.support.ClassPathXmlApplicationContext#0' defined in URL [file:/C:/Users/Giuseppe/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/RESTService4.0/WEB-INF/classes/beanRefContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/C:/Users/Giuseppe/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/RESTService4.0/WEB-INF/classes/projectPersistence-dao.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named projectPersistence
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

我还在 Maven 依赖项中包含了 hibernate-core 和 hibernate-entitymanager,并且默认情况下,persistence.xml 包含在类路径中。

任何帮助,将不胜感激。谢谢。

最佳答案

你的persistence.xml文件在哪里??它应该在某个 {classpath}/META-INF 目录中(例如,WEB-INF/classes/META-INF/或在你的 jar/META-INF 中)

编辑:

或者,如果您的文件在正确的位置,您的 xml 命名空间可能有问题,请尝试使用此样式 No Persistence provider for EntityManager named

关于spring - 没有命名 EntityManager 的持久性提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9564140/

相关文章:

spring - 使用 Spring boot 在单个浏览器 session 中使用多个 OAuth2 客户端

java - Hibernate 正确的级联映射

java - 你能帮助理解如何使用 JOIN 与 spring-repository 吗?

java - 同时保留两个实体

java - 无效的列类型 ~ 将 ArrayList<String> 发送到 pl/sql createdNameQuery

Spring WebApplicationInitializer 和 Jetty 8.x

java - 应用程序之间如何交换信号?

Spring + Hibernate + Tomcat 依赖问题

java - Thyemleaf 嵌套迭代触发 org.thymeleaf.exceptions.TemplateInputException

java - 如何在 persistence.xml 中设置默认的 Eclipselink CacheCooperativeType?