java - 类是托管的,但未在 persistence.xml 文件中列出

标签 java spring hibernate jpa persistence

我的项目出现以下异常:

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

我的 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>

我试过这个postthis ,两者都建议手动或自动将类添加到 persistence.xml。其他人说这是一个与 eclipse 相关的问题,可以通过清理或重新打开项目来解决。但是,我希望 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>

最佳答案

如果您使用的是 Eclipse:(1) 选择:(您的项目)-> 属性 -> JPA; (2) 寻找“Persistent class management”并选择“Discover annotated classes automatically”选项; (3) 按“应用”。

关于java - 类是托管的,但未在 persistence.xml 文件中列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24040208/

相关文章:

java - 将 Spring Java8 Junit 测试转换为 Kotlin 时摆脱 lateinit

java - 带有自定义缓存管理器实现的 Spring 缓存抽象不起作用

Hibernate SequenceStyleGenerator Preferred_sequence_per_entity

java - 从列表<>中提取信息

java - 使用外观设计模式|缺点/解决方案/建议

将 Grails 应用程序部署到 Tomcat 时出现 javax.websocket.DeploymentException

java - 与 DTO 关联的实体

java - 使用模拟测试时无法注入(inject)application.properties中定义的@value

java - 资源前面的过滤器中没有用户代理 (getAgent)

java - 在纯 JPA 设置中获取数据库连接