如何使用常见的JPA实体布局Java EE项目? [英] How to layout Java EE projects using common JPA entities?

查看:119
本文介绍了如何使用常见的JPA实体布局Java EE项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个eclipse Java EE 6项目使用maven 3打包在WAR文件中。现在他们应该在第三个项目中共享JPA实体,因为它们都使用相同的数据库。

I have two eclipse Java EE 6 projects packaged in a WAR-file using maven 3. Now they should share JPA entities in a 3rd project among them, since they both use the same database.

在对我的问题进行研究时,我发现了一些提示,例如在persistence.xml中引用了一个常见的jar,但我没有成功使它工作。
特别要求:

When doing the research for my question, I found some hints mentioning for example a reference in a persistence.xml to a common jar, but I wasn't successful to make it work. So specifically asked:

1)包含公共实体的项目是否有persistence.xml文件?如果是这样,它与其他项目的不同之处是什么?

1) Does the project, containing the common entities, has a persistence.xml file? If so, how does it differ from the one in the other projects?

2)两个项目中引用的常用实体到底有多准确?我是否在persistence.xml中使用该标记作为参考?如果是这样,如果MyCommin.jar位于WEB-INF / lib中,lib / MyCommon.jar是否正确使用?

2) How exactly are the commonly used entities referenced in the two projects? Do I use the tag in their persistence.xml for reference? And if so, would "lib/MyCommon.jar" be the right way to use, if MyCommin.jar is located in WEB-INF/lib?

3)如果将应用程序部署为JBoss 6中的WAR或爆炸存档,它会有什么不同吗?

3) Does it make any difference if the application is deployed as a WAR or an exploded archive in JBoss 6?

4)当通过添加到服务器运行时和发布时在eclipse中部署时,是有没有什么不同于部署外部eclipse?

4) When deployed within eclipse via addition to server runtime and publish, is there anything different from deploying outside eclipse?

5)所描述的方式是将公共实体放在一个单独的项目中,创建一个JAR并在另一个项目中使用该JAR是明智的处理共享公共实体类的问题的方法还是有更好的方法?

5) Is the described way putting common entities in a separate project, creating a JAR and use that JAR in the other project a sensible way to handle the problem of sharing common entity classes or is there a better way?

推荐答案


1)包含公共实体的项目是否具有persistence.xml文件?如果是这样,它与其他项目的不同之处是什么?

是的。主要区别在于模型项目中的 persistence.xml 应该定义所有实体并且 persistence.xml in Web项目应该只引用模型项目JAR文件。否则,你会在 persistence.xml 文件中保留重复的内容。

Yes. The main difference is that the persistence.xml in the model project should define all entities and that the persistence.xml in the web projects should just reference the model project JAR file. Otherwise you keep duplicating entites in persistence.xml files.

另一个可能的区别是模型项目中的那个应该是使用资源本地事务类型,以便从项目内部进行单元测试(例如,使用 main()方法)非常容易。 Web项目中的那些当然应该使用例如数据源。 JTA。

Another possible difference is that the one in model project should use a resource local transaction type so that unit testing from within the project (e.g. using a main() method) is done very easily. The ones in web projects should of course use a datasource by e.g. JTA.


2)在此引用的常用实体究竟是如何确切的?两个项目?我是否在persistence.xml中使用< jar-file> 标记作为参考?如果是这样,如果MyCommin.jar位于WEB-INF / lib中,lib / MyCommon.jar是否正确使用?

2) How exactly are the commonly used entities referenced in the two projects? Do I use the <jar-file> tag in their persistence.xml for reference? And if so, would "lib/MyCommon.jar" be the right way to use, if MyCommin.jar is located in WEB-INF/lib?

是和否。< jar-file> 是正确的方法,但你的文件名中有一个拼写错误,文件夹应该是被省略。 lib 文件夹通常仅在您拥有EAR并且JAR包含在EAR的 lib 文件夹中时使用。

Yes and No. The <jar-file> is the right way, but you have a typo in the filename and the folder should be omitted. The lib folder is usually only used when you've an EAR and the JAR is included in EAR's lib folder.

<jar-file>MyCommon.jar</jar-file>








3)如果应用程序在JBoss 6中部署为WAR或爆炸存档,它会有什么不同吗?

没有。正确设计的Java API不依赖于本地磁盘文件系统,而只依赖于类路径。如果WAR被扩展,它使类路径没有区别。

No. Properly designed Java API's don't rely on the local disk file system, but just on the classpath. It makes for the classpath no difference if the WAR is expanded or not.


4)当通过添加到服务器运行时和发布部署在eclipse中时,是否有任何不同于部署外部eclipse?

技术上,是的。在功能上,没有。

Technically, yes. Functionally, no.


5)所描述的方式是将公共实体放入一个单独的项目,创建一个JAR并在另一个项目中使用该JAR是一种处理共享公共实体类问题的合理方法,还是有更好的方法?

这绝对有道理。 您不想重复自己

这篇关于如何使用常见的JPA实体布局Java EE项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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