karim
karim

Reputation: 169

No bean named 'persistence-unit' is defined

I'm developing a spring jpa hibernate application and i have the following exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addressDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence-unit' is defined
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at ma.jobcenter.dao.Main.main(Main.java:21)

It says that no bean named 'persistence-unit' was found.

Here is my persistence.xml:

<persistence xmlns="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_2_0.xsd"
         version="2.0">

<persistence-unit name="persistence-unit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>ma.jobcenter.domain.Address</class>
    <class>ma.jobcenter.domain.Candidate</class>
    <class>ma.jobcenter.domain.RegistredUser</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test-db"/>
        <property name="hibernate.connection.username" value="postgres"/>
        <property name="hibernate.connection.password" value="root"/>
        <property name="show_sql" value="false"/>
    </properties>
</persistence-unit>

Here is my AbstractDAO that every DAO class is inheriting from:

public abstract class AbstractDAO {

@PersistenceContext(unitName = "persistence-unit")
protected EntityManager entityManager;

public AbstractDAO() {
    super();
}

public EntityManager getEntityManager() {
    return entityManager;
}

public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}
}

And this is my spring application-context.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:util="http://www.springframework.org/schema/util"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                       http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                       http://www.springframework.org/schema/tx
                       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
                       http://www.springframework.org/schema/util
                       http://www.springframework.org/schema/util/spring-util-4.3.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!--  tell spring where to find the beans -->
<context:component-scan base-package="ma.jobcenter.dao" />
<context:component-scan base-package="ma.jobcenter.service" />
<!--  tell spring to use annotation based congfigurations -->
<context:annotation-config/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <!-- access -->
    <property name="driverClass" value="org.postgresql.Driver" />
    <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/jobcenter-test-db" />
    <property name="user" value="postgres" />
    <property name="password" value="root" />

    <!-- pool sizing -->
    <property name="initialPoolSize" value="10" />
    <property name="minPoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="acquireIncrement" value="5" />
    <property name="maxStatements" value="100" />

    <!-- retries -->
    <property name="acquireRetryAttempts" value="20" />
    <property name="acquireRetryDelay" value="1000" /> <!-- 1s -->
    <property name="breakAfterAcquireFailure" value="false" />

    <!-- refreshing connections -->
    <property name="maxIdleTime" value="300" /> <!-- 5min -->
    <property name="maxConnectionAge" value="300" /> <!-- 1h -->

    <!-- timeouts and testing -->
    <property name="testConnectionOnCheckout" value="false" />
    <property name="testConnectionOnCheckin" value="false" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="ma.jobcenter.domain.*" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
        </bean>
    </property>
</bean>

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

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="addressDAO" class="ma.jobcenter.dao.AddressDAOImpl"/>
<bean id="candidateDAO" class="ma.jobcenter.dao.CandidateDAOImpl"/>
</beans>

The application is runing on Tomcat. I really don't understand this exception and i didn't found any answer. Thanks in advance.

Upvotes: 0

Views: 3713

Answers (2)

karim
karim

Reputation: 169

I found the solution, i had to change unitName="persistance-unit" by name="persistance-unit" in @PersistenceContext now it works fine.

Upvotes: 3

pleft
pleft

Reputation: 7915

Try putting your persistence.xml file in src/main/resources/META-INF and I guess you should delete the line <property name="persistenceXmlLocation" value="classpath:persistence.xml" /> in your application-context.xml

Also check if all your files in src/resources are copied in the WAR file deployed to Tomcat.

Upvotes: 0

Related Questions