在 .ear 文件中跨组件共享持久性单元
在我使用 .ear 打包的 Java EE 6 应用程序中,我想创建一个可以从不同 .jar 文件中的组件访问的持久性单元。
但是,我不确定如何定义这个持久性单元。使用 @PersistenceContext 注释,仅当名称与本地 persistence.xml 文件中定义的持久性单元匹配时,查找才会成功。
是否可以引用外部持久性单元?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
以下是 JPA 2.0 规范的相关部分:
后来:
总而言之,您应该能够在 EAR 的顶层定义您的实体和持久性单元,并在其他模块中使用它们。
我只是不确定您尝试了什么以及您遇到了什么问题。
Here are the relevant sections of the JPA 2.0 specification:
And later:
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.
该问题可以通过将 persistence.xml 放入位于ear 的lib 目录中的jar 文件中来解决。
persistence.xml 必须包含包含实体的 jar 文件。
我必须提供 jar 文件的相对路径。
我的耳朵目录结构
我的 jboss 7.1.1 的 persistence.xml
希望这有帮助
The problem could be solved by placing a persistence.xml in a jar file that is located in the ear's lib directory.
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
My persistence.xml for jboss 7.1.1
Hope this helps
所有你需要的
All you need
Glassfish 的工作 EAR 布局示例:
EJB 模块可以是 jar 档案或分解目录。
在这种情况下,您的persistence.xml可能类似于:
如果您使用模块版本控制(例如ejb1-module-),则必须更新
引用1.0-SNAPSHOT.jar)。带有@MappedSuperclass 注释和EntityManager 注入的抽象对象可以放置在任何外部jar 中。这个 jar 不需要在
persistence.xml
中提及。例如,您可以使用PersistableEntity.java
创建core-module.jar
:和
PersistableEntityManager.java
:此
core-module。 jar
可以被具有不同持久性单元的所有项目使用。您只需继承实体和 EJB 并避免样板文件。
查看 github 上的 bilionix-core 示例。
Example working EAR layout for Glassfish:
EJB modules may be either jar archives or exploded directories.
In this case your
persistence.xml
may be like:You have to update
<jar-file>
references if you use module versioning (e.g.ejb1-module-1.0-SNAPSHOT.jar
).Abstract objects with
@MappedSuperclass
annotation andEntityManager
injection may be placed in any external jar. This jar does not require to be mentioned inpersistence.xml
. For example, you can createcore-module.jar
withPersistableEntity.java
:And
PersistableEntityManager.java
: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.
尝试这样做:
以这种方式配置 EAR application.xml 文件:
http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd”
版本=“7”>
您的EE应用程序
在您的 EJB 项目 YourEJBModule1、YourEJBModule2... 和 YourEJBModuleX 中:
注入 Persistence Context,不带 unitName 属性:
YourEJBModule1:
YourEJBModule2:
YourEJBModuleX:
数据库中可以存在各种模式,每个 EJB 模块一个,通过 jta-data-source 访问它们
(部署在 Glassfish 4 中)
Try with this:
Configure EAR application.xml file in this way:
http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
version="7">
YourEEApplication
In your EJB projects YourEJBModule1, YourEJBModule2... and YourEJBModuleX:
Inject Persistence Context whitout the unitName property:
YourEJBModule1:
YourEJBModule2:
YourEJBModuleX:
In the database can exists various schemas, one per EJB module, access them through jta-data-source
(Deployed in Glassfish 4)
这就是我所做的。
1)将持久化配置文件打包为jar文件。 jar 文件将包含:
2) 打包另一个 jar 中的关联实体类:
持久性上下文现在应该可供企业应用程序中捆绑的所有 EJB 和 Web 应用程序访问。
Here's what I did.
1) Package the persistence configuration files in a jar file. The jar file will contain:
2) Package the associated entity classes in another jar:
The Persistence Context should now be accessible to all EJB and Web Apps bundled in your Enterprise Application.
我想在没有 EAR 项目的情况下实现共享持久性 EJB 模块。
这可以通过将
类,使用Have fun!
I wanted to achieve shared persistance EJB module without EAR project.
This is possible by
Have fun!