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

标签 hibernate jpa persistence java-ee-6 war

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项目,所以部署单元是一个war文件。 我尝试引用的 jar 文件位于 WEB-INF/lib/ 文件夹中,persistence.xml 位于 WEB-INF/classes/META-INF 文件夹。部署时,它只是告诉我

"WARNING: Unable to find file (ignored): file:.../../lib/app-services-1.0.jar".

我还尝试了我能想到的所有可能的路径,即../lib/app-services-1.0.jarlib/app-services-1.0.jar>.

执行此操作的正确方法是什么?

最佳答案

看看 jsr 总是有效的!

8.2.1.6.3 Jar Files

One or more JAR files may be specified using the jar-file elements instead of, or in addition to the mapping files specified in the mapping-file elements. If specified, these JAR files will >be searched for managed persistence classes, and any mapping metadata annotations found on them will be pro-cessed, or they will be mapped using the mapping annotation defaults defined by this specification. Such JAR files are specified relative to the directory or jar file that contains the root of the persis-tence unit.

The following examples illustrate the use of the jar-file element to reference additional persistence classes. These examples use the convention that a jar file with a name terminating in “PUnit” contains the persistence.xml file and that a jar file with a name terminating in “Entities” contains additional persistence classes.

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 文件的示例,上面示例中的所有 war 文件都在 Ear 文件内!
但是我在war文件中进行了测试,只有当我指定jar文件的绝对路径时它才有效,这对于生产环境来说不是一个好方法!

关于hibernate - 在 Web 应用程序中引用 jpa persistence.xml 中的 jar 文件的正确路径是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4433341/

相关文章:

java - Hibernate HQL 转换 java.lang.ClassCastException : java. lang.Integer 无法转换为 java.lang.Long

java - 如何使用 hibernate 使用现有对象创建复合键对象

jpa - 可用性矩阵建模的设计问题

memory - Redis 如何在我的本地 Apache 服务器上保存数据,即使在重启和完全断电后也是如此?

database - Docker-compose v3 不保留 postgres 数据库

java - 无法初始化代理 - 没有 session 和惰性对象

hibernate - 在 JPA 中,可嵌入值的映射,具有用作键的嵌入实体

java - 即使类路径中存在依赖项,Spring Boot也会显示 'can not be resolved'

java - 从 Spring Boot 应用程序源代码生成 ERD 图

java - 是否有与 Apple 的 Core Data 等效的 Java?