persistence.xml not found in Eclipse Maven Project

JuanBruno2013 picture JuanBruno2013 · Jan 3, 2014 · Viewed 24.4k times · Source

I cannot fix the persistence.xml file not found eclipse problem, this is a simple test project (Maven Nature) for a very basic EJB testing, the file is indeed in src/main/resources/META-INF/... this is the pom.xml contents. Tried adding the folder to the project's build path, updating maven project. No luck so far, any help would be greatly appreciated, thanks!

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>LibEE</groupId>
  <artifactId>LibEE</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>

      <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>main.resources.Main</mainClass>

</manifest>
</archive>
</configuration>
</plugin>
   </plugins>
  </build>

  <dependencies>
    <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.1.0</version>
    </dependency>

     <dependency>
    <groupId>org.ow2.spec.ee</groupId>
    <artifactId>ow2-ejb-3.0-spec</artifactId>
    <version>1.0.13</version>
</dependency>

<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>


  </dependencies>
</project>

And this is the persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">

<persistence-unit name="LibEE" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/LibEE</jta-data-source> <!-- Refers to data source in Enterprise server -->
    <class>main.java.entities.Book</class>
<properties>
    <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    <property name="eclipselink.logging.level" value="INFO"/>
</properties>


</persistence-unit>

</persistence>

Answer

Shekh Akther picture Shekh Akther · Jul 31, 2014

persistence.xml file MUST be added to the classpath to resovle this issue.

Try as following:

  1. Create/Update a directory src/main/resources/META-INF, place persistence.xml file here

  2. Run Maven->Update project configuration

  3. Verify that src/main/resources was added to source folders (Java Resources) by Right click on your project in Eclipse IDE > Properties > Java Build Path > Source tab. You will found /src/main/resources here.

  4. Select Maven->Update project configuration or Project->Clean

  5. If you build your applicaiton, check for persistence.xml file under target//WEB-INF/classes/META-INF/persistence.xml (or how you have configured your classpath).

  6. That's it!