我是否需要persistence.xml中的<class>元素?

Mic*_*ech 106 java orm annotations hibernate jpa

我有非常简单的persistance.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">

    <persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
        <class>pl.michalmech.eventractor.domain.User</class>
        <class>pl.michalmech.eventractor.domain.Address</class>
        <class>pl.michalmech.eventractor.domain.City</class>
        <class>pl.michalmech.eventractor.domain.Country</class>

        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>

</persistence>
Run Code Online (Sandbox Code Playgroud)

它的工作原理.

但是当我删除<class>元素时,应用程序看不到实体(所有类都带有注释@Entity).

是否有任何自动机制来扫描@Entity类?

Pas*_*ent 78

persistence.xml有一个jar-file你可以使用的.从Java EE 5教程:

<persistence>
    <persistence-unit name="OrderManagement">
        <description>This unit manages orders and customers.
            It does not rely on any vendor-specific features and can
            therefore be deployed to any persistence provider.
        </description>
        <jta-data-source>jdbc/MyOrderDB</jta-data-source>
        <jar-file>MyOrderApp.jar</jar-file>
        <class>com.widgets.Order</class>
        <class>com.widgets.Customer</class>
    </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

此文件定义名为的持久性单元OrderManagement,该单元使用支持JTA的数据源jdbc/MyOrderDB.所述jar-fileclass元素指定管持久类:实体类,可嵌入类和超类映射.该jar-file元素指定对包含托管持久性类的打包持久性单元可见的JAR文件,而该class元素显式指定托管持久性类.

在Hibernate的情况下,请看第2章.设置和配置也有更多详细信息.

编辑:实际上,如果你不介意不符合规范,Hibernate甚至在Java SE中也支持自动检测.为此,请添加hibernate.archive.autodetection属性:

<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
  <!-- This is required to be spec compliant, Hibernate however supports
       auto-detection even in JSE.
  <class>pl.michalmech.eventractor.domain.User</class>
  <class>pl.michalmech.eventractor.domain.Address</class>
  <class>pl.michalmech.eventractor.domain.City</class>
  <class>pl.michalmech.eventractor.domain.Country</class>
   -->

  <properties>
    <!-- Scan for annotated classes and Hibernate mapping XML files -->
    <property name="hibernate.archive.autodetection" value="class, hbm"/>

    <property name="hibernate.hbm2ddl.auto" value="validate" />
    <property name="hibernate.show_sql" value="true" />
  </properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)

  • 我明白了,但实体(@Entity)位于单独的Maven项目中,因此jar文件名可以在每次构建时更改.我正在寻找能够在特定包或类路径中扫描所有内容的东西.我只是懒得在persistence.xml文件中键入很多很多<class>元素. (13认同)
  • 我知道古老的线程,但看看[jpa-maven-plugin](http://github.com/ljnelson/jpa-maven-plugin). (5认同)

Mad*_*bæk 43

Java SE环境中,按照规范,您必须像以前一样指定所有类:

必须在Java SE环境中指定所有命名的托管持久性类的列表,以确保可移植性

如果不打算将持久性单元的根中包含的带注释的持久性类包含在持久性单元中,则应使用exclude-unlisted-classes元素.exclude-unlisted-classes元素不适用于Java SE环境.

(JSR-000220 6.2.1.6)

Java EE环境中,您不必执行此操作,因为提供程序会为您扫描注释.

非正式地,您可以尝试<exclude-unlisted-classes>false</exclude-unlisted-classes>在persistence.xml中进行设置.此参数默认为falseEE和trueSE.双方的EclipseLinkToplink的支持这是到目前为止,我可以告诉.但如上所述,根据规范,你不应该依赖它在SE工作.

您可以尝试以下操作(在SE环境中可能有效,也可能无效):

<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
     <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)

  • `&lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;` 不适用于 WildFly 8.2.1.Final + Hibernate 4.3.7 (2认同)

abb*_*bas 13

我是否需要persistence.xml中的Class元素?

不,你不一定.以下是您在Eclipse中的使用方法(Kepler测试):

右键单击项目,单击Properties,选择JPA,在Persistence类管理中自动选中Discover annotated classes.

在此输入图像描述

  • 为什么要upvote?OP甚至没有提到Eclipse,这个答案没有说明这个Eclipse功能在幕后做了什么,所以人们可以在没有IDE的情况下做到这一点. (8认同)
  • @Artem Novikov:我觉得这很苛刻,因为问题经常出现在不同的环境中,在这里我们希望提供帮助或提供有用的提示!(就像对我一样)它很有用,因为 Eclipse 是一个通用的 IDE,用于进行这样的开发,并且在幕后并不是那么重要,但我想它将包括所有相关的工作区项目(例如,我的项目取决于)。 (2认同)

小智 11

对于那些在Spring中运行JPA的人,从版本3.1开始,你可以设置packagesToScan属性LocalContainerEntityManagerFactoryBean并完全摆脱persistence.xml.

这是低位


小智 8

您可以提供jar-file具有已编译类的文件夹的元素路径.例如,当我为一些集成测试准备persistence.xml时,我添加了类似的东西:

 <jar-file>file:../target/classes</jar-file>
Run Code Online (Sandbox Code Playgroud)


eri*_*ooo 8

对于JPA 2+,这就是诀窍

 <jar-file></jar-file>
Run Code Online (Sandbox Code Playgroud)

扫描战争中所有罐子的注释@Entity类

  • 你有更多的信息吗?这是偶然的,还是写在规范中?它是否依赖于实施? (2认同)

Chu*_*ski 7

Hibernate不支持<exclude-unlisted-classes>false</exclude-unlisted-classes>SE,(另一张海报提到这适用于TopLink和EclipseLink).

有些工具可以自动生成persistence.xml的类列表,例如IntelliJ中的Import Database Schema向导.在persistence.xml中获得项目的初始类之后,随着项目的进展,手动添加/删除单个类应该很简单.