在Web应用程序中引用jpa persistence.xml中的jar文件的正确途径是什么?

Bob*_*obo 22 persistence hibernate jpa war java-ee-6

persistence.xml看起来像这样:

<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>jdbc/test</non-jta-data-source>
    <jar-file>../../lib/app-services-1.0.jar</jar-file>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)

它是一个Web项目,因此部署单元是一个war文件.我试图引用的jar文件在WEB-INF/lib /文件夹中,persistence.xmlWEB-INF/classes/META-INF文件夹中.部署时,它只是告诉我

"警告:无法找到文件(忽略):file:.../../lib/app-services-1.0.jar".

我也试过,我能想到的,即每一个可能的路径../lib/app-services-1.0.jar,LIB /应用服务-1.0.jar.

这样做的正确途径是什么?

Hei*_*deh 21

看看jsr总是有效!

8.2.1.6.3 Jar文件

可以使用jar-file元素来指定一个或多个JAR文件,而不是mapping-file元素中指定的映射文件,或者除了元素中指定的映射文件之外.如果指定,将搜索这些JAR文件以查找托管持久性类,并且将对其上找到的任何映射元数据注释进行处理,或者将使用此规范定义的映射注释默认值对它们进行映射.此类JAR文件是相对于包含persis-tence单元根目录的目录或jar文件指定的.

以下示例说明了使用该jar-file元素引用其他持久性类.这些示例使用以下惯例:名称以"PUnit"结尾的jar文件包含该persistence.xml文件,并且名称以"Entities"结尾的jar文件包含其他持久性类.

Example 1:
app.ear  
   lib/earEntities.jar  
   earRootPUnit.jar (with META-INF/persistence.xml )  
persistence.xml contains:  
   <jar-file>lib/earEntities.jar</jar-file>  

Example 2:
app.ear
   lib/earEntities.jar
   lib/earLibPUnit.jar (with META-INF/persistence.xml )
persistence.xml contains:
   <jar-file>earEntities.jar</jar-file>

Example 3:
app.ear
   lib/earEntities.jar
   ejbjar.jar (with META-INF/persistence.xml )
persistence.xml contains:
   <jar-file>lib/earEntities.jar</jar-file>

Example 4:
app.ear
    war1.war
       WEB-INF/lib/warEntities.jar
       WEB-INF/lib/warPUnit.jar (with META-INF/persistence.xml )
persistence.xml contains:
    <jar-file>warEntities.jar</jar-file>

Example 5:
app.ear
   war2.war
      WEB-INF/lib/warEntities.jar
      WEB-INF/classes/META-INF/persistence.xml
persistence.xml contains:
   <jar-file>lib/warEntities.jar</jar-file>

Example 6:
app.ear
    lib/earEntities.jar
    war2.war
    WEB-INF/classes/META-INF/persistence.xml
 persistence.xml contains:
    <jar-file>../../lib/earEntities.jar</jar-file>

Example 7:
app.ear
   lib/earEntities.jar
   war1.war
   WEB-INF/lib/warPUnit.jar (with META-INF/persistence.xml )
persistence.xml contains:
   <jar-file>../../../lib/earEntities.jar</jar-file>
Run Code Online (Sandbox Code Playgroud)

如您所见,没有war文件的示例,上面示例中的所有war文件都是内部文件!
但是我在war文件中进行了测试,它只是在我指定jar文件的绝对路径时才有效,对于生产环境来说它不是一个好方法!


小智 12

以防万一其他人偶然发现:jar-file-statement只有效.当持久性单元作为Enterprise Archives(.ear)的一部分进行部署时 - 在所有其他情况下(.war),persistence.xml必须驻留在/ META-INF /中,并且不能引用驻留在持久性单元之外的类(见:http://javahowto.blogspot.com/2007/06/where-to-put-persistencexml-in-web-app.html).所以,据我所知,没有办法让一个persistence.xml存在于WEB-INF/classes/META-INF中,它引用了不在WEB-INF/classes中的类.

  • 你回答是完全错误的.Entity类不能强制位于充当Persistence root的JAR文件中.一点也不.我知道在Weblogic,Glassfish和TomcatEE中,一个WAR文件可以完美地包含一个内部JAR文件,该文件包含META-INF/persistence.xml,并且通过使用jar可以非常简单地引用同一个lib文件夹中的其他jar文件.带有其他jar名称的file元素.您也可以在JPA 2.1 JSR的第367页和第368页中看到这种情况.话虽如此,在Wildfly 10中完成这项工作是一个挑战. (3认同)
  • 我认为就像耳朵文件一样,这也适用于战争文件,但在尝试了很多之后,我在这里看到了你的帖子,并明白战争文件是不可能的!但有趣的是,当我在文件系统中指定 jar 文件的“绝对路径”时,它起作用了!;) (2认同)

Pra*_* M 8

war2.war
      WEB-INF/lib/warEntities.jar
      WEB-INF/classes/META-INF/persistence.xml
persistence.xml contains:
   <jar-file>lib/warEntities.jar</jar-file>
Run Code Online (Sandbox Code Playgroud)

此格式适用于war文件.我使用Wildlfy 8.2.0和JPA 2.1

  • 也可用于Payara Server 4.1.1.162 #badassfish(内部版本116) (2认同)