在.ear文件中跨组件共享持久性单元 [英] Sharing a persistence unit across components in a .ear file
问题描述
然而,我不确定如何定义这个持久性单元。通过@PersistenceContext注释,只有当名称与本地persistence.xml文件中定义的持久性单元匹配时,查找才会成功。
是否可以引用外部持久性单元?
以下是JPA 2.0规范的相关部分:
8.2持久性单元打包
...
定义持久性单元通过
persistence.xml
文件。 jar文件或
目录的META-INF
目录
包含persistence.xml
文件是
,称为持久性
单元的根。在Java EE环境中,持久化单元的
root必须是以下
:
- 一个EJB-JAR文件
- WAR文件的WEB-INF / classes目录[80]
- WAR文件的WEB-INF / lib目录
- EAR库目录中的jar文件
- 应用程序客户机jar文件
不需要包含持久性单元
的EJB-JAR或
WAR文件被打包在一个EAR中,除非
持久化单元包含持久性
类以及那些在EJB-JAR或WAR中包含
的类。请参阅部分
8.2.1.6。
注意:Java Persistence 1.0支持在
EAR的根目录中使用jar文件一个持久性单元的根。
此用法不再受支持。
可移植应用程序应该为这种情况使用EAR库目录
,而不是。见[9]。
持久性单元必须有一个名称。
任何给定的
名称中只有一个持久化单元必须在单个
EJB-JAR文件中,单个WAR
文件内,单个应用程序
客户端中定义jar或EAR中。请参阅
第8.2.2节持久性单元
作用域。
persistence.xml 文件可以使用
来指定同一个作用域内的多个持久性
单元。
所有持久性类定义在Java EE EAR的
级别必须是应用程序中所有其他Java EE
组件可访问的
- 即应用程序classloader加载的
- 这样如果相同的实体类被两个不同的Java EE
组件(可能使用
不同的持久性单元)引用,
引用的类是相同的
类。
以后:
8.2。 2持久性单元作用域
EJB-JAR,WAR,应用程序客户端
jar或EAR可以定义持久性
单元。
当使用
uni引用持久性单元
注释
时tName
元素或持久性单元名称
部署描述符元素,
可见性范围持久性
单位由它的
定义点决定:
$ b $ ul
一个持久性单元,定义在EJB-JAR,WAR或
应用程序客户机jar的作用域为
,分别是EJB-JAR,WAR或应用程序jar
,并且对于在此定义的
组件可见jar或war。
在EAR级定义的持久性单元通常对
应用程序中的所有组件可见
。但是,如果在EAR内由
EJB-JAR,WAR或应用程序jar文件
定义持久性
同名单元,则持久性单元
在EAR级别定义的那个名称的
对于由该EJB-JAR,WAR或
应用程序jar文件定义的组件
不可见,除非
持久性单元引用使用
持久化单元名称#
语法到
指定消除
引用的路径名称。当#
语法是使用
,路径名是相对于
引用应用程序组件jar
文件。例如,语法
../ lib / persistenceUnitRoot.jar#myPersistenceUnit
指的是一个持久化单元,其名称为
,在persistence.xml
文件的名称元素
中,是
myPersistenceUnit
,
持久化单元的根的
相对路径名是
../ lib / persistenceUnitRoot.jar
。
#
语法可以与unitName
注释元素或
<$ c一起使用$ c $> persistence-unit-name 部署
描述符元素引用在EAR级别定义的
持久性单元。
您还需要将实体类jar包含在pu jar清单中 http://wiki.eclipse .org / Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG)
总而言之,您应该能够将您的实体和持久性单元定义为EAR的最高级别,并从其他模块中使用它们。
我只是不确定要了解您尝试的以及您遇到的问题。
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.
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?
Here are the relevant sections of the JPA 2.0 specification:
8.2 Persistence Unit Packaging
...
A persistence unit is defined by a
persistence.xml
file. The jar file or directory whoseMETA-INF
directory contains thepersistence.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:
- an EJB-JAR file
- the WEB-INF/classes directory of a WAR file[80]
- a jar file in the WEB-INF/lib directory of a WAR file
- a jar file in the EAR library directory
- an application client jar file
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.
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].
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".
The
persistence.xml
file may be used to designate more than one persistence unit within the same scope.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.
And later:
8.2.2 Persistence Unit Scope
An EJB-JAR, WAR, application client jar, or EAR can define a persistence unit.
When referencing a persistence unit using the
unitName
annotation element orpersistence-unit-name
deployment descriptor element, the visibility scope of the persistence unit is determined by its point of definition:
- 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 thepersistence.xml
file, ismyPersistenceUnit
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 theunitName
annotation element orpersistence-unit-name
deployment descriptor element to reference a persistence unit defined at EAR level.Also you need to include entity classes jar in manifest of pu jar http://wiki.eclipse.org/Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG)
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屋!