JPA persistence.xml META-INF not working correctly

Padawan picture Padawan · Jun 13, 2013 · Viewed 31.3k times · Source

I am stumped when it comes to the location of my persistence.xml file. I am using eclipselink 2.4 as my JPA 2.0 implementation and have my persistence.xml located under src/main/resources/META-INF/persistence.xml as many posts state.

The first issue: After publishing to my server inside eclipse I get an error stating that my persistence.xml is not found.

Exception: : java.lang.IllegalArgumentException: No persistence unit named 'X'
is available in scope Webapp. Available persistence units: [] at

enter image description here

Here is the code for my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="X" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>localJTA</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
    <property name="eclipselink.target-server" value="WebLogic"/>
    <property name="eclipselink.logging.level" value="FINEST"/>          
</properties>
</persistence-unit>
</persistence>

If I move the META-INF to webapp/WEB-INF/classes/META-INF then it works! I cannot understand what the problem is.

enter image description here

I checked the projects build path and everything looks good there.

enter image description here

The second issue I am having is that my annotated entity class is not being picked up automatically by eclipselink. In my JPA config I have Discover annotated classes automatically selected.

enter image description here

I also have the following in my xml

<exclude-unlisted-classes>false</exclude-unlisted-classes>

Technologies used: weblogic 12c server, EJB 3.1, JPA 2.0, M2E-WTP, and JSF 2.1

Update 1: Leaving META-INF on the path src/main/resources/META-INF/persistence.xml and performing maven install to build the war file, the META-INF folder is correctly placed in WEB-INF/classes/META-INF/persistence.xml like it should be (inside the war). Seems that it is only and issue of when the webapp is built and ran locally in Eclipse. Can anyone explain this?

Update 2: Leaving META-INF in src/main/resources i.e src/main/resources/META-INF/persistence.xml and the following in my persistence.xml rather than explicitly listing the entity class

<exclude-unlisted-classes>false</exclude-unlisted-classes>

works IF I build the war and deploy it through the weblogic console via internet explorer. Why doesn't it work inside of eclipse!?

Update 3:
did you inspect what gets built -Yes, inside Eclipse I have build set to automatically. Looking in the in the project folder I see java package com, META-INF folder containing persistence.xml, and resources folder which are under Webapp/target/classes.
If the war is the same -Are you referring to the Webapp.war folder found in C:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\Weblogic12cDomain_auto_generated_ear_\Webapp.war ? If so the only thing in there is WEB-INF\lib\jsf-impl-2.1.7.jar
then did you inspect the classpath being used to test your app in Eclipse- This entry is found in my classpath as expected under Webapp/.classpath
if the persistence.xml in the resource directory is on the classpath first, it will be picked up instead of the one in your war or one in the correct directory - My understanding is Eclipse will building the project, then copy the contents to the server without generating a war file.

Here is what is at the root of the maven generated Webapp.war:

enter image description here

Here is what is at the root of the Webapp project folder:

enter image description here

Here is what is at the root of the Webapp.war FOLDER under C:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\Weblogic12cDomain_auto_generated_ear_\Webapp.war

enter image description here

if the persistence.xml in the resource directory is on the classpath first, it will be picked up instead of the one in your war or one in the correct directory.- Not sure what you mean by this. The persistence.xml in my resource/META-INF is the one I want picked up. Not sure what a war file has to do with it? Either way they should be the same persistence.xml I would think.

Any help is MUCH appreciated!!

Answer

Chris picture Chris · Jun 13, 2013

Section 8.2.1 of the JPA spec states "The persistence.xml file is located in the META-INF directory of the root of the persistence unit." and later "The managed persistence classes may either be contained within the root of the persistence unit; or they may be specified by reference—i.e., by naming the classes, class archives, or XML mapping files (which in turn reference classes) that are accessible on the application classpath"

So the root of the persistence unit is where the META-INF directory directory is. By placing the classes in a classes directory, they are not in the root of the persistence unit, and so must be explicitly named to be included.