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.
在我正在使用.ear打包的Java EE 6应用程序中,我想创建一个可以从不同.jar文件中的组件访问的持久性单元。
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.
但是,我不确定如何定义此持久性单元。使用@PersistenceContext注释,只有在名称与本地persistence.xml文件中定义的持久性单元匹配时,查找才会成功。
Is it possible to refer to external persistence units?
是否可以引用外部持久性单元?
7 个解决方案
#1
45
Here are the relevant sections of the JPA 2.0 specification:
以下是JPA 2.0规范的相关部分:
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:持久性单元由persistence.xml文件定义。 META-INF目录包含persistence.xml文件的jar文件或目录称为持久性单元的根。在Java EE环境中,持久性单元的根必须是以下之一:
- an EJB-JAR file
EJB-JAR文件
- the WEB-INF/classes directory of a WAR file[80]
WAR文件的WEB-INF / classes目录[80]
- a jar file in the WEB-INF/lib directory of a WAR file
WAR文件的WEB-INF / lib目录中的jar文件
- a jar file in the EAR library directory
EAR库目录中的jar文件
- an application client jar file
应用程序客户端jar文件
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.
除非持久性单元包含除EJB-JAR或WAR中包含的持久性类之外的持久性类,否则不要求将包含持久性单元的EJB-JAR或WAR文件打包在EAR中。见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].
注意:Java Persistence 1.0支持在EAR的根目录中使用jar文件作为持久性单元的根。不再支持此用途。对于这种情况,便携式应用程序应该使用EAR库目录。见[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”.
持久性单元必须具有名称。必须在单个EJB-JAR文件中,单个WAR文件内,单个应用程序客户机jar内或EAR中定义任何给定名称的一个持久性单元。请参见第8.2.2节“持久性单元范围”。
The
persistence.xml
file may be used to designate more than one persistence unit within the same scope.persistence.xml文件可用于在同一范围内指定多个持久性单元。
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.
在Java EE EAR级别定义的所有持久性类必须可由应用程序中的所有其他Java EE组件访问 - 即由应用程序类加载器加载 - 这样,如果相同的实体类由两个不同的Java EE组件引用(可能使用不同的持久性单元),引用的类是相同的相同类。
And later:
8.2.2 Persistence Unit Scope
An EJB-JAR, WAR, application client jar, or EAR can define a persistence unit.
EJB-JAR,WAR,应用程序客户机jar或EAR可以定义持久性单元。
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:使用unitName注释元素或persistence-unit-name部署描述符元素引用持久性单元时,持久性单元的可见性范围由其定义点确定:
- 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.
在EJB-JAR,WAR或应用程序客户机jar级别定义的持久性单元分别限定为该EJB-JAR,WAR或应用程序jar,并且对该jar或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.在EAR级别定义的持久性单元通常对应用程序中的所有组件可见。但是,如果EAR中的EJB-JAR,WAR或应用程序jar文件定义了同名的持久性单元,那么在EAR级别定义的该名称的持久性单元将不会对该EJB定义的组件可见 - JAR,WAR或应用程序jar文件,除非持久性单元引用使用持久性单元名称#语法来指定路径名以消除引用的歧义。使用#语法时,路径名相对于引用应用程序组件jar文件。例如,语法../lib/persistenceUnitRoot.jar#myPersistenceUnit是指持久性单元,其名称(在persistence.xml文件的name元素中指定)是myPersistenceUnit,并且其根的相对路径名称为持久性单元是../lib/persistenceUnitRoot.jar。 #语法可以与unitName注释元素或persistence-unit-name部署描述符元素一起使用,以引用在EAR级别定义的持久性单元。
Also you need to include entity classes jar in manifest of pu jar http://wiki.eclipse.org/Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG)
你还需要在pu jar的清单中包含实体类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.
总而言之,您应该能够在EAR的顶层定义实体和持久性单元,并从其他模块中使用它们。
I'm just not sure to understand what you tried and what problem(s) you faced.
我只是不确定你理解你尝试了什么以及你遇到了什么问题。
#2
12
The problem could be solved by placing a persistence.xml in a jar file that is located in the ear's lib directory.
可以通过将persistence.xml放在ear的lib目录中的jar文件中来解决此问题。
The persistence.xml must contain the jar files which includes the Entities. I had to give the relative path to the jar files. My ear irectory structure
persistence.xml必须包含包含实体的jar文件。我必须给出jar文件的相对路径。我的耳朵结构
|-ear--
|-lib--|... some libs ...
| |--my-persistence-xml.jar
|-ejb-with-entities1.jar
|-ejb-with-entities2.jar
My persistence.xml for jboss 7.1.1
我的jboss 7.1.1的persistence.xml
<persistence-unit name="my-pu" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/mypu</jta-data-source>
<jar-file>../ejb-with-entities1.jar</jar-file>
<jar-file>../ejb-with-entities1.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
Hope this helps
希望这可以帮助
#3
7
All you need
一切你需要的
EAR +
|- META-INF +
| - persistence.xml
|- ejb1-module.jar
|- ejb2-module.jar
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="my-persistence-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>MyDataSource</jta-data-source>
<!-- Note: it's relative to `persistence-module.jar` file location in EAR -->
<jar-file>../ejb1-module.jar</jar-file>
<jar-file>../ejb2-module.jar</jar-file>
<properties>
...
</properties>
</persistence-unit>
</persistence>
#4
5
Example working EAR layout for Glassfish:
Glassfish的EAR布局示例:
EAR +
|- lib +
| |- core-module.jar
| \- persistence-module.jar +
| \- META-INF +
| \- persistence.xml
|- ejb1-module.jar
\- ejb2-module.jar
EJB modules may be either jar archives or exploded directories.
EJB模块可以是jar存档或展开目录。
In this case your persistence.xml
may be like:
在这种情况下,您的persistence.xml可能类似于:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="my-persistence-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>MyDataSource</jta-data-source>
<!-- Note: it's relative to `persistence-module.jar` file location in EAR -->
<jar-file>../ejb1-module.jar</jar-file>
<jar-file>../ejb2-module.jar</jar-file>
<properties>
<property name="hibernate.current_session_context_class" value="jta"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
You have to update <jar-file>
references if you use module versioning (e.g. ejb1-module-1.0-SNAPSHOT.jar
).
如果使用模块版本控制(例如ejb1-module-1.0-SNAPSHOT.jar),则必须更新
Abstract objects with @MappedSuperclass
annotation and EntityManager
injection may be placed in any external jar. This jar does not require to be mentioned in persistence.xml
. For example, you can create core-module.jar
with PersistableEntity.java
:
具有@MappedSuperclass注释和EntityManager注入的抽象对象可以放置在任何外部jar中。 persistence.xml中不需要提及此jar。例如,您可以使用PersistableEntity.java创建core-module.jar:
public class PersistableEntity {
@Id
@GeneratedValue
private Long id;
public Long getId() { return id; }
public Integer getVersion() { return version; }
}
And PersistableEntityManager.java
:
public class PersistableEntityManager<T extends PersistableEntity> {
@PersistenceContext
protected EntityManager em;
}
This core-module.jar
could be used by all your projects with different persistence units. You just inherit your Entities and EJBs and avoid boilerplate. Check out example bilionix-core on github.
这个core-module.jar可以被具有不同持久性单元的所有项目使用。您只需继承您的实体和EJB并避免使用样板。查看github上的示例bilionix-core。
#5
1
Try with this:
试试这个:
-
Configure EAR application.xml file in this way:
以这种方式配置EAR application.xml文件:
http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" version="7"> YourEEApplication
http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd“version =”7“> YourEEApplication
<initialize-in-order>true</initialize-in-order> <!-- This is the most important thing --> <module> <ejb>YourEJBModule1.jar</ejb> </module> <module> <ejb>YourEJBModule2.jar</ejb> </module> ...... <module> <ejb>YourEJBModuleX.jar</ejb> </module> <module> <web> <web-uri>YourWebModule.war</web-uri> <context-root>YourWebModule</context-root> </web> </module>
-
In your EJB projects YourEJBModule1, YourEJBModule2... and YourEJBModuleX:
在您的EJB项目YourEJBModule1,YourEJBModule2 ...和YourEJBModuleX中:
Inject Persistence Context whitout the unitName property:
注入持久性上下文whitout unitName属性:
@PersistenceContext(type=PersistenceContextType.TRANSACTION)
private EntityManager em; // get & set
- For each EJB module persistence.xml file:
对于每个EJB模块persistence.xml文件:
YourEJBModule1:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="PersistenceUnit1"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/YourDataSource</jta-data-source>
<class>com.example.Foo1</class>
<!-- Other properties -->
</persistence-unit>
</persistence>
YourEJBModule2:
<?xml version="1.0" encoding="UTF-8"?>
...
<persistence-unit name="PersistenceUnit2"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/YourDataSource</jta-data-source>
<jar-file>YourEJBModule1.jar</jar-file>
<class>com.example.Foo2</class>
<!-- Other properties -->
</persistence-unit>
...
YourEJBModuleX:
<?xml version="1.0" encoding="UTF-8"?>
...
<persistence-unit name="PersistenceUnitX"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/YourDataSource</jta-data-source>
<jar-file>YourEJBModule1.jar</jar-file>
<jar-file>YourEJBModule2.jar</jar-file>
......
<class>com.example.FooX</class>
<!-- Other properties -->
</persistence-unit>
...
In the database can exists various schemas, one per EJB module, access them through jta-data-source
在数据库中可以存在各种模式,每个EJB模块一个,通过jta-data-source访问它们
(Deployed in Glassfish 4)
(部署在Glassfish 4中)
#6
-1
Here's what I did.
这就是我做的。
1) Package the persistence configuration files in a jar file. The jar file will contain:
1)将持久性配置文件打包在jar文件中。 jar文件将包含:
- META-INF/persistence.xml (and orm.xml if you use one)
- Create a "lib" folder under the EAR project and stick the jar in there
META-INF / persistence.xml(如果使用的话还有orm.xml)
在EAR项目下创建一个“lib”文件夹并将jar放在那里
2) Package the associated entity classes in another jar:
2)将关联的实体类打包到另一个jar中:
- Put this jar file in your GlassFish domain/lib folder (or whatever lib folder equiv in other servers)
- I initially bundled the jar in the EAR's "lib" folder but classes were not found
- If anyone knows a better way to handle this please explain
将此jar文件放在GlassFish域/ lib文件夹中(或其他服务器中的任何lib文件夹)
我最初将jar捆绑在EAR的“lib”文件夹中,但是找不到类
如果有人知道更好的方法来处理这个,请解释
The Persistence Context should now be accessible to all EJB and Web Apps bundled in your Enterprise Application.
现在,企业应用程序中捆绑的所有EJB和Web应用程序都可以访问持久性上下文。
#7
-1
I wanted to achieve shared persistance EJB module without EAR project.
我想在没有EAR项目的情况下实现共享持久性EJB模块。
This is possible by
这是可能的
- moving all persistance entities to separate EJB project (do not move persistance.xml to new project, only classes are needed
- compiling this EJB project
- sending project to GlassFish server using
将所有持久性实体移动到单独的EJB项目(不要将persistance.xml移动到新项目,只需要类
编译这个EJB项目
使用将项目发送到GlassFish服务器
scpAsSudo ~/NetbeansProjects/UnifyEntities/dist/UnifyEntities.jar remoteUsername@192.168.0.1:/opt/glassfish3/domains/domain1/lib/ext
Have fun!
#1
45
Here are the relevant sections of the JPA 2.0 specification:
以下是JPA 2.0规范的相关部分:
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:持久性单元由persistence.xml文件定义。 META-INF目录包含persistence.xml文件的jar文件或目录称为持久性单元的根。在Java EE环境中,持久性单元的根必须是以下之一:
- an EJB-JAR file
EJB-JAR文件
- the WEB-INF/classes directory of a WAR file[80]
WAR文件的WEB-INF / classes目录[80]
- a jar file in the WEB-INF/lib directory of a WAR file
WAR文件的WEB-INF / lib目录中的jar文件
- a jar file in the EAR library directory
EAR库目录中的jar文件
- an application client jar file
应用程序客户端jar文件
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.
除非持久性单元包含除EJB-JAR或WAR中包含的持久性类之外的持久性类,否则不要求将包含持久性单元的EJB-JAR或WAR文件打包在EAR中。见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].
注意:Java Persistence 1.0支持在EAR的根目录中使用jar文件作为持久性单元的根。不再支持此用途。对于这种情况,便携式应用程序应该使用EAR库目录。见[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”.
持久性单元必须具有名称。必须在单个EJB-JAR文件中,单个WAR文件内,单个应用程序客户机jar内或EAR中定义任何给定名称的一个持久性单元。请参见第8.2.2节“持久性单元范围”。
The
persistence.xml
file may be used to designate more than one persistence unit within the same scope.persistence.xml文件可用于在同一范围内指定多个持久性单元。
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.
在Java EE EAR级别定义的所有持久性类必须可由应用程序中的所有其他Java EE组件访问 - 即由应用程序类加载器加载 - 这样,如果相同的实体类由两个不同的Java EE组件引用(可能使用不同的持久性单元),引用的类是相同的相同类。
And later:
8.2.2 Persistence Unit Scope
An EJB-JAR, WAR, application client jar, or EAR can define a persistence unit.
EJB-JAR,WAR,应用程序客户机jar或EAR可以定义持久性单元。
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:使用unitName注释元素或persistence-unit-name部署描述符元素引用持久性单元时,持久性单元的可见性范围由其定义点确定:
- 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.
在EJB-JAR,WAR或应用程序客户机jar级别定义的持久性单元分别限定为该EJB-JAR,WAR或应用程序jar,并且对该jar或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.在EAR级别定义的持久性单元通常对应用程序中的所有组件可见。但是,如果EAR中的EJB-JAR,WAR或应用程序jar文件定义了同名的持久性单元,那么在EAR级别定义的该名称的持久性单元将不会对该EJB定义的组件可见 - JAR,WAR或应用程序jar文件,除非持久性单元引用使用持久性单元名称#语法来指定路径名以消除引用的歧义。使用#语法时,路径名相对于引用应用程序组件jar文件。例如,语法../lib/persistenceUnitRoot.jar#myPersistenceUnit是指持久性单元,其名称(在persistence.xml文件的name元素中指定)是myPersistenceUnit,并且其根的相对路径名称为持久性单元是../lib/persistenceUnitRoot.jar。 #语法可以与unitName注释元素或persistence-unit-name部署描述符元素一起使用,以引用在EAR级别定义的持久性单元。
Also you need to include entity classes jar in manifest of pu jar http://wiki.eclipse.org/Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG)
你还需要在pu jar的清单中包含实体类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.
总而言之,您应该能够在EAR的顶层定义实体和持久性单元,并从其他模块中使用它们。
I'm just not sure to understand what you tried and what problem(s) you faced.
我只是不确定你理解你尝试了什么以及你遇到了什么问题。
#2
12
The problem could be solved by placing a persistence.xml in a jar file that is located in the ear's lib directory.
可以通过将persistence.xml放在ear的lib目录中的jar文件中来解决此问题。
The persistence.xml must contain the jar files which includes the Entities. I had to give the relative path to the jar files. My ear irectory structure
persistence.xml必须包含包含实体的jar文件。我必须给出jar文件的相对路径。我的耳朵结构
|-ear--
|-lib--|... some libs ...
| |--my-persistence-xml.jar
|-ejb-with-entities1.jar
|-ejb-with-entities2.jar
My persistence.xml for jboss 7.1.1
我的jboss 7.1.1的persistence.xml
<persistence-unit name="my-pu" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/mypu</jta-data-source>
<jar-file>../ejb-with-entities1.jar</jar-file>
<jar-file>../ejb-with-entities1.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
Hope this helps
希望这可以帮助
#3
7
All you need
一切你需要的
EAR +
|- META-INF +
| - persistence.xml
|- ejb1-module.jar
|- ejb2-module.jar
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="my-persistence-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>MyDataSource</jta-data-source>
<!-- Note: it's relative to `persistence-module.jar` file location in EAR -->
<jar-file>../ejb1-module.jar</jar-file>
<jar-file>../ejb2-module.jar</jar-file>
<properties>
...
</properties>
</persistence-unit>
</persistence>
#4
5
Example working EAR layout for Glassfish:
Glassfish的EAR布局示例:
EAR +
|- lib +
| |- core-module.jar
| \- persistence-module.jar +
| \- META-INF +
| \- persistence.xml
|- ejb1-module.jar
\- ejb2-module.jar
EJB modules may be either jar archives or exploded directories.
EJB模块可以是jar存档或展开目录。
In this case your persistence.xml
may be like:
在这种情况下,您的persistence.xml可能类似于:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="my-persistence-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>MyDataSource</jta-data-source>
<!-- Note: it's relative to `persistence-module.jar` file location in EAR -->
<jar-file>../ejb1-module.jar</jar-file>
<jar-file>../ejb2-module.jar</jar-file>
<properties>
<property name="hibernate.current_session_context_class" value="jta"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
You have to update <jar-file>
references if you use module versioning (e.g. ejb1-module-1.0-SNAPSHOT.jar
).
如果使用模块版本控制(例如ejb1-module-1.0-SNAPSHOT.jar),则必须更新
Abstract objects with @MappedSuperclass
annotation and EntityManager
injection may be placed in any external jar. This jar does not require to be mentioned in persistence.xml
. For example, you can create core-module.jar
with PersistableEntity.java
:
具有@MappedSuperclass注释和EntityManager注入的抽象对象可以放置在任何外部jar中。 persistence.xml中不需要提及此jar。例如,您可以使用PersistableEntity.java创建core-module.jar:
public class PersistableEntity {
@Id
@GeneratedValue
private Long id;
public Long getId() { return id; }
public Integer getVersion() { return version; }
}
And PersistableEntityManager.java
:
public class PersistableEntityManager<T extends PersistableEntity> {
@PersistenceContext
protected EntityManager em;
}
This core-module.jar
could be used by all your projects with different persistence units. You just inherit your Entities and EJBs and avoid boilerplate. Check out example bilionix-core on github.
这个core-module.jar可以被具有不同持久性单元的所有项目使用。您只需继承您的实体和EJB并避免使用样板。查看github上的示例bilionix-core。
#5
1
Try with this:
试试这个:
-
Configure EAR application.xml file in this way:
以这种方式配置EAR application.xml文件:
http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" version="7"> YourEEApplication
http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd“version =”7“> YourEEApplication
<initialize-in-order>true</initialize-in-order> <!-- This is the most important thing --> <module> <ejb>YourEJBModule1.jar</ejb> </module> <module> <ejb>YourEJBModule2.jar</ejb> </module> ...... <module> <ejb>YourEJBModuleX.jar</ejb> </module> <module> <web> <web-uri>YourWebModule.war</web-uri> <context-root>YourWebModule</context-root> </web> </module>
-
In your EJB projects YourEJBModule1, YourEJBModule2... and YourEJBModuleX:
在您的EJB项目YourEJBModule1,YourEJBModule2 ...和YourEJBModuleX中:
Inject Persistence Context whitout the unitName property:
注入持久性上下文whitout unitName属性:
@PersistenceContext(type=PersistenceContextType.TRANSACTION)
private EntityManager em; // get & set
- For each EJB module persistence.xml file:
对于每个EJB模块persistence.xml文件:
YourEJBModule1:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="PersistenceUnit1"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/YourDataSource</jta-data-source>
<class>com.example.Foo1</class>
<!-- Other properties -->
</persistence-unit>
</persistence>
YourEJBModule2:
<?xml version="1.0" encoding="UTF-8"?>
...
<persistence-unit name="PersistenceUnit2"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/YourDataSource</jta-data-source>
<jar-file>YourEJBModule1.jar</jar-file>
<class>com.example.Foo2</class>
<!-- Other properties -->
</persistence-unit>
...
YourEJBModuleX:
<?xml version="1.0" encoding="UTF-8"?>
...
<persistence-unit name="PersistenceUnitX"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/YourDataSource</jta-data-source>
<jar-file>YourEJBModule1.jar</jar-file>
<jar-file>YourEJBModule2.jar</jar-file>
......
<class>com.example.FooX</class>
<!-- Other properties -->
</persistence-unit>
...
In the database can exists various schemas, one per EJB module, access them through jta-data-source
在数据库中可以存在各种模式,每个EJB模块一个,通过jta-data-source访问它们
(Deployed in Glassfish 4)
(部署在Glassfish 4中)
#6
-1
Here's what I did.
这就是我做的。
1) Package the persistence configuration files in a jar file. The jar file will contain:
1)将持久性配置文件打包在jar文件中。 jar文件将包含:
- META-INF/persistence.xml (and orm.xml if you use one)
- Create a "lib" folder under the EAR project and stick the jar in there
META-INF / persistence.xml(如果使用的话还有orm.xml)
在EAR项目下创建一个“lib”文件夹并将jar放在那里
2) Package the associated entity classes in another jar:
2)将关联的实体类打包到另一个jar中:
- Put this jar file in your GlassFish domain/lib folder (or whatever lib folder equiv in other servers)
- I initially bundled the jar in the EAR's "lib" folder but classes were not found
- If anyone knows a better way to handle this please explain
将此jar文件放在GlassFish域/ lib文件夹中(或其他服务器中的任何lib文件夹)
我最初将jar捆绑在EAR的“lib”文件夹中,但是找不到类
如果有人知道更好的方法来处理这个,请解释
The Persistence Context should now be accessible to all EJB and Web Apps bundled in your Enterprise Application.
现在,企业应用程序中捆绑的所有EJB和Web应用程序都可以访问持久性上下文。
#7
-1
I wanted to achieve shared persistance EJB module without EAR project.
我想在没有EAR项目的情况下实现共享持久性EJB模块。
This is possible by
这是可能的
- moving all persistance entities to separate EJB project (do not move persistance.xml to new project, only classes are needed
- compiling this EJB project
- sending project to GlassFish server using
将所有持久性实体移动到单独的EJB项目(不要将persistance.xml移动到新项目,只需要类
编译这个EJB项目
使用将项目发送到GlassFish服务器
scpAsSudo ~/NetbeansProjects/UnifyEntities/dist/UnifyEntities.jar remoteUsername@192.168.0.1:/opt/glassfish3/domains/domain1/lib/ext
Have fun!