我的项目中出现以下异常:
Class" com.testApp.domain.Register"是托管的,但未列在persistence.xml文件中
我的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>
我已尝试过此post和this,两者都建议手动或自动将该类添加到persistence.xml
。其他人说这是一个与日食有关的问题,可以通过清理或重新开放项目来解决。但是,我希望这些类可以通过hibernate自动检测,而不是手动将它们添加到我的persistence.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>
答案 0 :(得分:46)
如果您正在使用Eclipse:(1)选择:(您的项目) - &gt;属性 - &gt; JPA; (2)寻找&#34;持久的班级管理&#34;并选择选项&#34;自动发现带注释的类&#34 ;; (3)按&#34;应用&#34;。
答案 1 :(得分:4)
如果是Eclipse问题,请转到:
Preferences >> Java Persistence >> JPA >> Errors/Warnings >> Type
在以下文件中,标记为Ignore
:
答案 2 :(得分:3)
如果你使用Spring,你可以避免使用persistence.xml
文件,而是声明一个像这样的实体管理器工厂:
<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>
com.testApp.domain
包中的所有实体(标有@Entity
注释)都将被加载,而不会以XML格式声明它们。
更新您需要声明数据源,例如:
<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>
然后,您可以完全按照惯例使用entityManager
。我想你在DAO类中使用类似的东西:
@PersistenceContext
private EntityManager em;