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

38,136

Solution 1

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;

Solution 2

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".

Solution 3

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
Share:
38,136
Carol.Kar
Author by

Carol.Kar

Updated on July 20, 2022

Comments

  • Carol.Kar
    Carol.Kar almost 2 years

    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>