Five ways to fix the 'no persistence.xml file found' error in Eclipse

No persistence.xml file found fix

The hardest part about learning Hibernate and JPA 3 is getting your first project up and running.

For those who encounter the dreaded ‘no persistence.xml file found’ error when running your first Hibernate and JPA project in Eclipse or IntelliJ, here are the five most common solutions:

  1. The persistence.xml file is misspelled or cased incorrectly
  2. The persistence.xml file is not located in a folder named META-INF
  3. The META-INF folder is not on the project’s classpath
  4. You don’t have a persistence.xml file
  5. Your Hibernate libraries are not set with compile scope in the Maven POM file

Double-check your spelling of persistence.xml

If the persistence.xml file is spelled incorrectly, has the wrong file extension, or is cased incorrectly, the ‘no persistence.xml file found’ error will appear.

The name, extension and casing of the persistence.xml file is non-negotiable. Make sure it’s spelled and cased correctly.

Check the folder location

The persistence.xml file must be located in a folder named META-INF.

It’s not good enough for the persistence.xml file to simply be on the classpath. It must be in the META-INF folder.

As with the persistence.xml file, the META-INF folder must be spelled exactly as the specification requires, with all of the letters upper cased.

Check your classpath

Sometimes the META-INF folder, especially if it’s placed in the \resources folder, will not be included in the project’s classpath.

To rectify this problem in Eclipse, simply right-click on the resources folder and select ‘Add to Classpath

Update Hibernate and JPA in the POM

When you copy the Maven coordinates of the Hibernate libraries that implement the JPA 3 spec, the type property is set to POM, and the scope is not provided.

Remove the type property and add a compile-time scope for your Hibernate libraries in the POM file, and the ‘no persistence.xml file found’ error should go away.

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>6.2.2.Final</version>
  <scope>compile</scope>
</dependency>

You don’t have a persistence.xml file

Sometimes the most obvious solution a problem is the one you overlook.

If the persistence.xml file was accidentally deleted, then it’s definitely not going to be found at runtime.

If it has gone missing, check your git repository and perform a restore. You need your persistence.xml file in order to run your Hibernate and JPA applications.

Sample persistence.xml file

If you are missing a persistence.xml file, or you need an updated JPA 3 persistence.xml file, here’s a sample one.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence 
             https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">

  <persistence-unit name="jpa-example"> 
    <properties>
      <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
      <property name="jakarta.persistence.jdbc.url"    value="jdbc:mysql://localhost:3306/hibernate_examples" />
      <property name="jakarta.persistence.jdbc.user"   value="hibernate-admin" />
      <property name="jakarta.persistence.jdbc.password" value="jpa3-password" />
      <property name="jakarta.persistence.schema-generation.database.action" value="create-and-drop" />

    </properties>
  </persistence-unit>
</persistence>

JPA 3.1 persistence.xml support

While this version explicitly references version 3, JPA 3.1 APIs will also be supported in your code so long as the underlying JPA 3.x provider supports them. Hibernate 6.2.2 is an example of a jPA 3.1 compliant implementation.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close