Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class is managed, but is not listed in the persistence.xml file

I am getting the following exception in my project:

Class "com.testApp.domain.Register" is managed, but is not listed in the persistence.xml file

My persistence.xml file looks like that:

<?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="default" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.detection" value="class, hbm" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />

            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:." />
            <property name="hibernate.connection.username" value="SA" />
            <property name="hibernate.connection.password" value="" />

            <property name="hibernate.c3p0.min_size" value="5" />
            <property name="hibernate.c3p0.max_size" value="20" />
            <property name="hibernate.c3p0.timeout" value="300" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.idle_test_period" value="3000" />

        </properties>
    </persistence-unit>

</persistence>

I have tried this post and this, both suggest to add the class manually or automatically to the persistence.xml. Others say its a eclipse related issue and can be solved by cleaning or reopening of the project. However, I want that these classes get autodetected by hibernate and not manually add them to my persistence.xml.

Any suggestions for a solution and to solve my error?

I appreciate your reply!

UPDATE

My applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
                        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
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/jee 
                        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                        http://www.springframework.org/schema/security
                        http://www.springframework.org/schema/security/spring-security-3.1.xsd">

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

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="default" />
    </bean>

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

    <bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

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

</beans>
like image 824
Carol.Kar Avatar asked Jun 04 '14 14:06

Carol.Kar


People also ask

Where is the persistence xml file?

If you package the persistence unit as a set of classes in a WAR file, persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory.

What is the purpose of the persistence xml file?

xml File. This file is used to override the default Hibernate settings and to add support for database types that are not out of the box (OOB database types are Oracle Server, Microsoft SQL Server, and MySQL).

How do I edit persistence xml?

Procedure. In the Package Explorer view, right-click the persistence. xml file of the JPA project that you want to edit and select Open with > Persistence XML Editor. In the Design tab of the Persistence XML Editor, make any of the following changes to the persistence.


3 Answers

If you are using Eclipse:(1) Select: (Your Project) -> Properties -> JPA; (2) Look for "Persistent class management" and select the option "Discover annotated classes automatically"; (3) Press "Apply".

like image 137
gleitonfranco Avatar answered Sep 20 '22 17:09

gleitonfranco


If it is an Eclipse problem, go to:

Preferences >> Java Persistence >> JPA >> Errors/Warnings >> Type

in the following itens, mark then as Ignore:

  • Class is annotated, but is not listed in the persistence.xml file
  • Class is managed, but is not listed in the persistence.xml file
like image 37
PedroPK Avatar answered Sep 23 '22 17:09

PedroPK


If you are using Spring you could avoid persistence.xml file and declare instead an entity manager factory like this:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="yourDataSource" />
    <property name="packagesToScan" value="com.testApp.domain" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
        </bean>
    </property>
</bean>

All the entities in com.testApp.domain package (marked with @Entity annotation) will be loaded without declaring them in XML.

UPDATE You need to declarate a data source, for example:

<bean id="yourDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:." />
    <property name="username" value="SA" />
    <property name="password" value="" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="20" />
</bean>

Then you can use the entityManager exactly as you were doing. I suppose you are using something similar in your DAO classes:

@PersistenceContext
private EntityManager em;
like image 31
davioooh Avatar answered Sep 21 '22 17:09

davioooh