No Persistence provider for EntityManager named

observer picture observer · Mar 5, 2012 · Viewed 20.9k times · Source

I got the error "No Persistence provider for EntityManager named projectPersistence org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288) " while I'm running my project on the server. Here's my configuration files:

persistence.xml

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

and, 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);
    }

}

This is the log of my console:

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)

I also included hibernate-core and hibernate-entitymanager in Maven dependencies and the persistence.xml is included in the classpath by default.

Any help would be appreciated. Thanks.

Answer

František Hartman picture František Hartman · Mar 5, 2012

Where do you have your persistence.xml file?? It should be in some {classpath}/META-INF directory (e.g, WEB-INF/classes/META-INF/ or in your jar/META-INF)

EDIT:

or if you have your file on the right plase there may be a problem with your xml namespacing, try to use this style No Persistence provider for EntityManager named