Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类是托管的,但未列在persistence.xml文件中_Java_Spring_Hibernate_Jpa_Persistence - Fatal编程技术网

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

Java 类是托管的,但未列在persistence.xml文件中,java,spring,hibernate,jpa,persistence,Java,Spring,Hibernate,Jpa,Persistence,我在项目中遇到以下异常: 类“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

我在项目中遇到以下异常:

类“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>

如果您使用的是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>
然后,您可以完全按照原来的方式使用
entityManager
。我想您在DAO类中使用了类似的东西:

@PersistenceContext
private EntityManager em;
如果您正在使用Eclipse:(1)选择:(您的项目)->Properties->JPA; (2) 查找“持久类管理”并选择“自动发现带注释的类”选项;
(3) 按“应用”。

如果是Eclipse问题,请转到:

Preferences >> Java Persistence >> JPA >> Errors/Warnings >> Type
在以下ITEN中,将其标记为
忽略

  • 类已注释,但未在persistence.xml文件中列出
  • 类是托管的,但未列在persistence.xml文件中

谢谢你的回答!我在更新中添加了我的
applicationContext.xml
。在您的情况下,
entitiyManagerFactory
仍然需要哪些属性?此外,我必须在我的DAO方法中调用它,对吗?这种方法的优点是什么?谢谢你的回答!你只需要用你的DB参数声明一个数据源(我更新了我的答案)。谢谢你的答案!
hsqldb
的数据源是什么?只需将表达式替换为参数。。。完成。谢谢你的回答!然而,这种方法是特定于IDE的。它对我有效(Eclipse Java EE IDE for Web Developers 4.4.2)。它工作得很好,只是在将我的google app engine项目从开普勒更新到mars之后出现。对我来说,它适用于Rational Application Developer for Websphere 8.5.5.7(IBM Eclipse)。谢谢,谢谢。这在Eclipse2019-06中适用,但它不是一个通用的首选项,因此我必须首先为JPA启用项目特定的首选项并将其设置在那里。对于Eclipse2020-09,右键单击项目->属性->JPA,选择选项“自动发现带注释的类”。
@PersistenceContext
private EntityManager em;
Preferences >> Java Persistence >> JPA >> Errors/Warnings >> Type