what is the right path to refer a jar file in jpa persistence.xml in a web app?
Solution 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.
Solution 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 themapping-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 thepersistence.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!
Solution 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
Related videos on Youtube
Bobo
Updated on July 09, 2022Comments
-
Bobo almost 2 years
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?
-
Matt Ball over 13 yearsWhat server are you running your app on? JBoss? GlassFish? If it's JBoss, you shouldn't need to specify Hibernate at all; it's the default.
-
Bobo over 13 yearsGlassFish, Matt. My problem is not about Hibernate or not, it is about how to tell JPA provider the path to the jar file in order to auto scan entities.
-
-
Bobo over 13 yearsTried the simple "app-services-1.0.jar" path and as you suspected, it does not work. So I guess the question is for war files, that <jar-file> just can not be used?
-
Mehdi about 11 yearsI thought that like ear files, this is possible for war files too but after trying a lot I saw your post here and understand that it is not possible for war files! but the interesting thing is that when I specify the 'absolute path' of jar files in file system, it works! ;)
-
Archimedes Trajano over 10 yearsThis is the best answer because it gives examples of what to put in for the jar-file element. Verified it to work as well.
-
Jin Kwon almost 8 yearsalso works with Payara Server 4.1.1.162 #badassfish (build 116)
-
99Sono over 7 yearsYou answer is completely incorrect. The Entity classes must not forcefully be inside of the JAR file that acts as Persistence root. Not At all. I know for a fact that in Weblogic, Glassfish and TomcatEE a WAR file can perfectly contain an inner JAR file that holds the META-INF/persistence.xml and quite trivially refer to other jar files in the same lib folder by using the jar-file element with the name of the other jars. You can see as well in page 367 and 368 of JPA 2.1 JSR that this is the case. With that said, making this work in Wildfly 10 is prooving to be a challenge.
-
99Sono over 7 yearsDownload the JSR from here: download.oracle.com/otndocs/jcp/persistence-2_1-fr-eval-spec/…
-
Francis about 7 years@Heidarzadeh how does one now inject an EntityManager referencing the persistence unit name inside earLibPUnit.jar , none of the examples show that
-
morgwai about 6 yearsdoesn't work for me when using hibernate-5.2.17 as JPA provider running inside Jetty-9.4.10, openJDK-8 :(