java - 在 .ear 文件中跨组件共享持久性单元

标签 java jpa jakarta-ee persistence glassfish

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

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

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

最佳答案

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

8.2 Persistence Unit Packaging

...

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:

  • 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.

后来:

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 or persistence-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 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.

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 的顶层定义您的实体和持久性单元,并在其他模块中使用它们。

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

关于java - 在 .ear 文件中跨组件共享持久性单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4073635/

相关文章:

java - SharedPreferences 上的 NullPointerException (似乎是上下文)

Java 使用高虚拟内存

java - 如何停止liferay中的缓存?

java.lang.ClassCastException : Application1 cannot be cast to Application2

java - Spring Data Redis 原子整型变量键名称

java - JPA 将实体的多对多字段值替换为不同的值

java - JPA @OneToOne 具有相同类型

java - ORM 使用什么来访问数据库

java - 名称中的上下文非法 : java:app/jdbc/ActionBazaarDS

java - JMX 定义