在persistence.xml之外定义jpa实体类 [英] define jpa entity classes outside of persistence.xml

查看:116
本文介绍了在persistence.xml之外定义jpa实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在persistence.xml之外定义jpa实体类(即在单独的文件中)?

Is there a way to define jpa entity classes outside of persistence.xml (i.e. in a separate file)?

能够不将persistence.xml作为一个外部文件也足够了。

Being able to not have persistence.xml as an external file would also suffice.

提前致谢,
史蒂文

Thanks in advance, Steven

编辑:
对不起,我不清楚。这是在Java SE环境中。
另外,我想在persistence.xml文件中没有some.class.AClass的列表。
这是因为我想动态创建这个类列表并引用包含此列表的文件。

Sorry I was not clear. This is in a Java SE environment. Also, I would like to not have a listing of some.class.AClass in my persistence.xml file. This is because I would like to create this list of classes dynamically and reference a file containing this list.

Edit2:
管理来解决这个问题通过在打包之前在构建时编写persistence.xml文件。如果有人感兴趣,我使用scannotations来发现所有使用@Entity注释的类,并将它们写入target / classes / META-INF中的persistence.xml文件(使用maven)。

Managed to solve this by writing a persistence.xml file at build time before it is packaged. If anyone is interested, I used scannotations to discover all the classes annotated with @Entity and wrote them to the persistence.xml file in target/classes/META-INF (using maven).

推荐答案

在Java SE环境中,可移植应用程序必须 persistence.xml中明确列出类。从JPA 1.0规范:

In a Java SE environment, portable applications must list classes explicitly in the persistence.xml. From the JPA 1.0 specification:


6.2.1.6 mapping-file,jar-file,class,exclude-unlisted-classes



以下类必须是
隐式或显式表示为
托管持久化类为
包含在持久性单元中:
实体类;可嵌入的课程;
映射超类。

6.2.1.6 mapping-file, jar-file, class, exclude-unlisted-classes

The following classes must be implicitly or explicitly denoted as managed persistence classes to be included within a persistence unit: entity classes; embeddable classes; mapped superclasses.

由持久性单元
管理的托管持久性类
的集合是通过使用以下
中的一个或多个来定义的:

The set of managed persistence classes that are managed by a persistence unit is defined by using one or more of the following:


  • 一个或多个对象/关系映射XML文件

  • 一个或多个jar文件将会搜索类

  • 类的明确列表


  • 持久性单元的根中包含的带注释的托管持久性类(除非
    exclude-unlisted-classes 元素指定为

  • One or more object/relational mapping XML files
  • One or more jar files that will be searched for classes
  • An explicit list of the classes
  • The annotated managed persistence classes contained in the root of the persistence unit (unless the exclude-unlisted-classes element is specified)

(...)

也可以指定名为托管持久性
类的列表,而不是
或另外到,JAR文件
和映射文件。将处理在这些
类中找到的任何映射
元数据注释,或者使用映射
注释默认值映射
。类元素
用于列出托管持久性
类。 必须在Java SE环境中为
指定所有已命名的托管
持久性类的列表,以确保
可移植性。便携式Java SE
应用程序不应该依赖于此处描述的
其他机制来为
指定持久性单元的托管持久性
类。
持久性提供者还可能需要
,要管理的实体类和
类的集合必须是每个
<$ c $中完全枚举的
c> persistence.xml Java SE
环境中的文件。

A list of named managed persistence classes may also be specified instead of, or in addition to, the JAR files and mapping files. Any mapping metadata annotations found on these classes will be processed, or they will be mapped using the mapping annotation defaults. The class element is used to list a managed persistence class. A list of all named managed persistence classes must be specified in Java SE environments to insure portability. Portable Java SE applications should not rely on the other mechanisms described here to specify the managed persistence classes of a persistence unit. Persistence providers may also require that the set of entity classes and classes that are to be managed must be fully enumerated in each of the persistence.xml files in Java SE environments.

$ b的根目录中包含的所有类$ b持久性单元也被搜索
用于带注释的托管持久性
类,并且在它们上找到的任何映射元数据
注释将被
处理,或者它们将被映射
使用映射注释默认值。
如果不打算将
持久性单元的根中包含的
带注释的持久性类
包含在
持久性单元中,则
exclude-unlisted-classes 元素
应该被使用。
exclude-unlisted-classes 元素是
,不适用于Java SE
环境。

All classes contained in the root of the persistence unit are also searched for annotated managed persistence classes and any mapping metadata annotations found on them will be processed, or they will be mapped using the mapping annotation defaults. If it is not intended that the annotated persistence classes contained in the root of the persistence unit be included in the persistence unit, the exclude-unlisted-classes element should be used. The exclude-unlisted-classes element is not intended for use in Java SE environments.

由持久性单元管理的
的实体集合是这些源的union
,映射
元数据注释(或注释
默认值)对于任何给定的类是
,如果同时存在
注释以及该类的XML映射
,则由XML映射
信息文件覆盖。最低便携式
级别的覆盖是持久字段或属性

The resulting set of entities managed by the persistence unit is the union of these sources, with the mapping metadata annotations (or annotation defaults) for any given class being overridden by the XML mapping information file if there are both annotations as well as XML mappings for that class. The minimum portable level of overriding is at the level of the persistent field or property.

如果可移植性不关心,某些提供程序确实支持Java SE环境中的实体发现(例如,EclipseLink, Hibernate )。

If portability is not a concern, some provider do support entity discovery in a Java SE environment (for example, EclipseLink, Hibernate).

如果担心可移植性,使用像Spring这样的第三方容器会有所帮助。

If portability is a concern, using a third party container like Spring would help.

这篇关于在persistence.xml之外定义jpa实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆