No persistence.xml file found fix
The hardest part about learning Hibernate and JPA 3 is how to get your first project up and running.
Some will encounter the dreaded no persistence.xml file found error when running their first Hibernate and JPA project in Eclipse or IntelliJ. Here are the five most common reasons for that, and by implication how to fix them:
- The persistence.xml file is misspelled or cased incorrectly.
- The persistence.xml file is not located in a folder named META-INF.
- The META-INF folder is not on the project’s classpath.
- You don’t have a persistence.xml file.
- 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 appears.
The name, extension and casing of the persistence.xml file are 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, you must spell the META-INF folder exactly as the specification requires, with all of the letters uppercased.
Check your classpath
Sometimes the META-INF folder, especially if it’s placed in the \resources folder, is not 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. 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 to a problem is the one you overlook.
If you accidentally delete the persistence.xml file, then it’s definitely not going to be found at runtime.
If the file has gone missing, check your git repository and perform a restore. You need that persistence.xml file 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, your code also will support JPA 3.1 APIs 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.