JPA persistence.xml META-INF not working correctly
Asked Answered
S

4

14

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

Saliva answered 13/6, 2013 at 2:52 Comment(0)
R
5

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.

Reasoning answered 13/6, 2013 at 13:24 Comment(4)
Thx Chris! this make sense, however it doesn't not work inside eclipse! =/ Please see my updates.Saliva
Chris can you please change your answer to a comment so that others do not think the question has been answered?Saliva
If its not the one you seek and you want more exposure, place a bounty on it, or do what you did - ask again with more or different information. You mention that this only occurs "when building in Eclipse" - did you inspect what gets built? If the war is the same, then did you inspect the classpath being used to test your app in Eclipse? 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.Reasoning
Hey Chris, please see Update 3 in response to your comments. Thanks again!Saliva
D
5

I believe you have at least 2 issues here.

1) Maven build

Your project has maven structure not Eclipse web project structure, so it must be build using maven commands, not Eclipse build by default. (I'm not maven expert, so please maven experts jump in and comment, if not true). So when you are doing automatic build in Eclipse it doesn't know that should copy your src/main/resources/META-INF/persistence.xml to the WEB-INF/classes/META-INF/persistence.xml and just compiles java classes.

2) Deployment

If you follow the Eclipse project structure, then Eclipse knows how to pack and add your project to the server, when you select 'Add to server'. If you are using maven, then your built war is usually in target directory. So again you would either need to export this war and install it using WebLogic tools, or install it via maven task.

Check WebLogic Server with Maven and Eclipse how Oracle suggests to use Eclipse with WebLogic and Maven

Desdemona answered 3/7, 2014 at 22:35 Comment(1)
I have Tomcat7(OpenJPA2.x) and IBM WAS 8.5.5(OpenJPA 2.x), webapp is non-jta(resource_local) jpa project. Tomcat was fine but WAS failed with the same war package. Placing xml to "mywebapp/WEB-INF/classes/META-INF/persistence.xml" did the trick, now both works fine.Carrnan
T
2

You never mention maven so I don't know if that's on your plate; it wasn't working for me with maven until I added the hibernate-entitymanager dependency. My pom.xml only had hibernate-core. After I added the hibernate-entitymanager then it started working (for my test).

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.1.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.1.Final</version>
</dependency>
Tribadism answered 25/2, 2014 at 21:3 Comment(0)
S
0

Check if the persistence.xml is inside your war. Or it is not or it is but in a wrong location. You can also place this file inside java/main/resources folder so it will be placed in your classpath

Selimah answered 3/7, 2014 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.