what is the right path to refer a jar file in jpa persistence.xml in a web app?

折月煮酒 提交于 2020-01-08 17:15:11

问题


persistence.xml looks like this:

<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>

It is a web project, so the deployment unit is a war file. The jar file I tried to refer is in WEB-INF/lib/ folder , persistence.xml is in WEB-INF/classes/META-INF folder. When being deployed, it simply tells me

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

I also tried every possible path I could think of, i.e. ../lib/app-services-1.0.jar, lib/app-services-1.0.jar.

What is the right path to do this?


回答1:


just in case someone else stumbles upon this: the jar-file-statement is only valid. when the persistence-unit is deployed as part of an Enterprise Archives (.ear) - in every other case (.war), the persistence.xml must reside in /META-INF/ and cannot refrence classes that live outside the persistence-unit (see: http://javahowto.blogspot.com/2007/06/where-to-put-persistencexml-in-web-app.html). So, as far as I know, there is no way to have a persistence.xml living in WEB-INF/classes/META-INF that references classes that do not live in WEB-INF/classes.




回答2:


Taking a look at jsr always works!

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>

As you see there is no example for war files, all war files in the examples above are inside ear files!
But I tested in war files and it works just when I specify the absolute path of jar files and it is not a good approach for production environment!




回答3:


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

this format works for war file. Im using Wildlfy 8.2.0 and JPA 2.1




回答4:


Not sure this is related to the fact that you are deploying as a WAR, but the path should be simply "app-sevices-1.0.jar" and the jar should be in the lib of the Java EE application. The thing is: I'm not sure if this is available for the simplified "war" Java EE application. I suspect this is available only for the traditional Java EE deployment file (ear). I would test making an EAR, containing a WAR for the webapp, JAR for the PU and your other JAR for app-services, like a traditional Java EE deployment would be.

Another thing to note is that relative paths are not supported, and usage of this in SE environment is not supported by all vendors.



来源:https://stackoverflow.com/questions/4433341/what-is-the-right-path-to-refer-a-jar-file-in-jpa-persistence-xml-in-a-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!