Class is managed, but is not listed in the persistence.xml file
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".
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
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:
@PersistenceContextprivate EntityManager em;