在 .ear 文件中跨组件共享持久性单元 [英] Sharing a persistence unit across components in a .ear file

查看:15
本文介绍了在 .ear 文件中跨组件共享持久性单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用 .ear 打包的 Java EE 6 应用程序中,我想创建一个可以从不同 .jar 文件中的组件访问的持久性单元.

In a Java EE 6 application where I'm using .ear packaging, I'd like to create a persistence unit that can be accessed from components in different .jar files.

但是,我不确定如何定义这个持久化单元.使用@PersistenceContext 注释,只有当名称与本地persistence.xml 文件中定义的持久性单元匹配时,查找才会成功.

However, I'm not sure how to define this persistence unit. With the @PersistenceContext annotation the lookup only succeeds if the name matches a persistence unit defined in the local persistence.xml file.

是否可以引用外部持久化单元?

Is it possible to refer to external persistence units?

推荐答案

以下是 JPA 2.0 规范的相关部分:

Here are the relevant sections of the JPA 2.0 specification:

...

一个持久化单元由一个persistence.xml 文件.jar 文件或META-INF 目录包含 persistence.xml 文件是被称为坚持的根源单元.在 Java EE 环境中,持久化单元的根必须是一以下内容:

A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit. In Java EE environments, the root of a persistence unit must be one of the following:

  • 一个 EJB-JAR 文件
  • WAR 文件的 WEB-INF/classes 目录[80]
  • WAR 文件的 WEB-INF/lib 目录中的 jar 文件
  • EAR 库目录中的一个 jar 文件
  • 一个应用程序客户端 jar 文件

EJB-JAR 或包含持久性单元的 WAR 文件包装在 EAR 中,除非持久性单元包含持久性除了包含的那些类在 EJB-JAR 或 WAR 中.见章节8.2.1.6.

It is not required that an EJB-JAR or WAR file containing a persistence unit be packaged in an EAR unless the persistence unit contains persistence classes in addition to those contained within the EJB-JAR or WAR. See Section 8.2.1.6.

注意:Java Persistence 1.0 支持在根目录中使用 jar 文件EAR 作为持久性单元的根.不再支持这种用途.对于这种情况,便携式应用程序应该使用 EAR 库目录而是.见[9].

NOTE: Java Persistence 1.0 supported use of a jar file in the root of the EAR as the root of a persistence unit. This use is no longer supported. Portable applications should use the EAR library directory for this case instead. See [9].

一个持久化单元必须有一个名字.任何给定的只有一个持久性单元名称必须在单个EJB-JAR 文件,在单个 WAR 中文件,在单个应用程序中客户端 jar,或在 EAR 中.看第 8.2.2 节,持久性单元范围".

A persistence unit must have a name. Only one persistence unit of any given name must be defined within a single EJB-JAR file, within a single WAR file, within a single application client jar, or within an EAR. See Section 8.2.2, "Persistence Unit Scope".

可以使用 persistence.xml 文件指定多个持久性同一范围内的单位.

The persistence.xml file may be used to designate more than one persistence unit within the same scope.

所有持久化类定义在Java EE EAR 的级别必须是可供所有其他 Java EE 访问应用程序中的组件 - 即由应用程序类加载器加载- 如果同一个实体类被两个不同的 Java EE 引用组件(可能正在使用不同的持久性单元),引用的类是相同的班级.

All persistence classes defined at the level of the Java EE EAR must be accessible to all other Java EE components in the application - i.e. loaded by the application classloader - such that if the same entity class is referenced by two different Java EE components (which may be using different persistence units), the referenced class is the same identical class.

后来:

一个 EJB-JAR、WAR、应用程序客户端jar 或 EAR 可以定义持久性单位.

8.2.2 Persistence Unit Scope

An EJB-JAR, WAR, application client jar, or EAR can define a persistence unit.

引用持久化单元时使用 unitName 注释元素或 persistence-unit-name部署描述符元素,持久化的可见范围单位由它的点决定定义:

When referencing a persistence unit using the unitName annotation element or persistence-unit-name deployment descriptor element, the visibility scope of the persistence unit is determined by its point of definition:

  • 在 EJB-JAR、WAR 或应用程序客户端 jar 的范围是EJB-JAR、WAR 或应用程序 jar分别对该 jar 或 war 中定义的组件.
  • 在 EAR 级别定义的持久性单元通常是对所有组件可见应用.但是,如果一个坚持同名单位由一个EJB-JAR、WAR 或应用程序 jar 文件在 EAR 中,持久性单元在 EAR 级别定义的名称将对组件不可见由 EJB-JAR、WAR 或应用程序 jar 文件,除非持久性单元引用使用持久化单元名称 # 语法为指定一个路径名来消除歧义# 语法是使用,路径名是相对于引用应用程序组件 jar文件.例如,语法../lib/persistenceUnitRoot.jar#myPersistenceUnit指一个持久化单元,它的name,在 name 元素中指定persistence.xml 文件,是myPersistenceUnit 并且对于根目录的相对路径名持久性单位是../lib/persistenceUnitRoot.jar.这# 语法可以与 unitName 注释元素或persistence-unit-name 部署要引用的描述符元素在 EAR 级别定义的持久性单元.
  • A persistence unit that is defined at the level of an EJB-JAR, WAR, or application client jar is scoped to that EJB-JAR, WAR, or application jar respectively and is visible to the components defined in that jar or war.
  • A persistence unit that is defined at the level of the EAR is generally visible to all components in the application. However, if a persistence unit of the same name is defined by an EJB-JAR, WAR, or application jar file within the EAR, the persistence unit of that name defined at EAR level will not be visible to the components defined by that EJB-JAR, WAR, or application jar file unless the persistence unit reference uses the persistence unit name # syntax to specify a path name to disambiguate the reference. When the # syntax is used, the path name is relative to the referencing application component jar file. For example, the syntax ../lib/persistenceUnitRoot.jar#myPersistenceUnit refers to a persistence unit whose name, as specified in the name element of the persistence.xml file, is myPersistenceUnit and for which the relative path name of the root of the persistence unit is ../lib/persistenceUnitRoot.jar. The # syntax may be used with both the unitName annotation element or persistence-unit-name deployment descriptor element to reference a persistence unit defined at EAR level.

您还需要在 pu jar 的清单中包含实体类 jar http://wiki.eclipse.org/Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG))

Also you need to include entity classes jar in manifest of pu jar http://wiki.eclipse.org/Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG)

总而言之,您应该能够在 EAR 的顶层定义您的实体和持久性单元,并在其他模块中使用它们.

To summarize, you should be able to define your entities and the persistence unit at the top level of the EAR and to use them from the other modules.

我只是不确定您尝试了什么以及您遇到了什么问题.

I'm just not sure to understand what you tried and what problem(s) you faced.

这篇关于在 .ear 文件中跨组件共享持久性单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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