Genaut
Genaut

Reputation: 1851

No bean named 'persistenceUnit' is defined

Im starting a new proyect with spring, jpa, hibernate and mysql database, but I am having a problem with this configuration.

I developed with a similar architecture, but I never created it, I am fighting with this.

This my dispatcher-servlet.xml

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

<!-- DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/editor" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.editor.entity.FolderEntity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<context:annotation-config/>
<context:component-scan base-package="com.editor" />

This my main DAO persistence config.

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

I have a running mysql database running in my machine. I have this persistence.xml file in my machine.

META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
    <persistence-unit name="persistenceUnit" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.gamecreator.editor.entity.FolderEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/gamecreator"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

I have this spring and persistence configuration, and my tag persistenceUnit in the DAO, but this not working. I dont know if I "connected" in the correct way this elements.

I read a lot about this topic, but I can't make this working. Any help? Each tutorial about this topic is different.

If I comment my persistence.xml file, I get the same error, I think that this file isn't loaded.

Upvotes: 1

Views: 1840

Answers (1)

JamesB
JamesB

Reputation: 7894

Your annotation is wrong. It should be

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

Upvotes: 1

Related Questions