Java 类受管理,但未列在 persistence.xml 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24040208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class is managed, but is not listed in the persistence.xml file
提问by Anna.Klee
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
类“com.testApp.domain.Register”被管理,但未在persistence.xml文件中列出
My persistence.xml
file looks like that:
我的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="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 postand 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
.
我已经尝试过这篇文章和这篇文章,都建议手动或自动将类添加到persistence.xml
. 其他人说这是一个与日食相关的问题,可以通过清理或重新打开项目来解决。但是,我希望这些类被休眠自动检测到,而不是手动将它们添加到我的persistence.xml
.
Any suggestions for a solution and to solve my error?
关于解决方案和解决我的错误的任何建议?
I appreciate your reply!
我很感激你的回复!
UPDATE
更新
My applicationContext.xml
:
我的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>
采纳答案by davioooh
If you are using Spring you could avoid persistence.xml
file and declare instead an entity manager factory like this:
如果您使用的是 Spring,您可以避免persistence.xml
file 并声明一个实体管理器工厂,如下所示:
<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.
com.testApp.domain
包中的所有实体(用@Entity
注释标记)将被加载,而无需在 XML 中声明它们。
UPDATEYou need to declarate a data source, for example:
UPDATE你需要声明一个数据源,例如:
<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:
然后你可以entityManager
完全按照你的方式使用。我想您在 DAO 类中使用了类似的东西:
@PersistenceContext
private EntityManager em;
回答by gleitonfranco
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".
如果您使用的是 Eclipse:(1) 选择:(您的项目)-> 属性 -> JPA;(2)找到“持久化类管理”,选择“自动发现带注释的类”选项;(3) 按“申请”。
回答by PedroPK
If it is an Eclipse problem, go to:
如果是 Eclipse 问题,请转到:
Preferences >> Java Persistence >> JPA >> Errors/Warnings >> Type
in the following itens, mark then as Ignore
:
在以下项目中,将 then 标记为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
- 类已注释,但未列在persistence.xml 文件中
- 类受管理,但未列在 persistence.xml 文件中