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

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> 

这是一个Web项目,所以部署单位是一个战争文件。 我尝试引用的jar文件位于WEB-INF / lib /文件夹中, persistence.xml位于WEB-INF / classes / META-INF文件夹中。 部署时,它只是告诉我

“警告:无法find文件(忽略):file:… / .. / lib / app-services-1.0.jar”。

我也尝试了所有可能的path,比如../lib/app-services-1.0.jar,lib / app-services-1.0.jar

什么是正确的道路做到这一点?

以防其他人绊倒在这: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 )。 所以,就我所知,没有办法在WEB-INF / classes / META-INF中拥有一个persistence.xml,它引用了不在WEB-INF / classes中的类。

看看JSR总是工作!

8.2.1.6.3 Jar文件

可以使用jar-file元素来指​​定一个或多个JAR文件,而不是mapping-file元素中指定的映射文件之外的映射文件。 如果指定了这些JAR文件,将search托pipe的持久化类,并且在这些JAR文件中find的任何映射元数据注释都将被处理,否则将使用本规范定义的映射注解默认值进行映射。 这样的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> 

正如你所看到的,没有战争文件的例子,上面例子中的所有war文件都是在ear文件中的!
但是我在war文件中testing过,并且它在我指定jar文件的绝对path时工作,这对于生产环境来说不是一个好的方法!

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

这个格式适用于war文件。 我使用Wildlfy 8.2.0和JPA 2.1

不确定这是否与您部署为WAR相关,但path应该是“app-sevices-1.0.jar”,而jar应该位于Java EE应用程序的lib中。 问题是:我不确定这是否适用于简化的“战争”Java EE应用程序。 我怀疑这只适用于传统的Java EE部署文件(ear)。 我会testing一个EAR,包含一个WAR的Web应用程序,JAR的PU和你的其他JAR的应用程序服务,就像传统的Java EE部署一样。

另外要注意的是不支持相对path,所有供应商都不支持在SE环境中使用此path。