- Details
- Written by Nam Ha Minh
- Last Updated on 09 April 2022   |   Print Email
In this post, I’d like to share some solutions that fix the following error in Java development with Hibernate/JPA:
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named XXX
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
where XXX is the persistence unit name declared in the persistence.xml file of your project, something like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>
<persistence-unit name="BookstoreWebsite">
<properties>
...
</properties>
</persistence-unit>
</persistence>
Here are 4 different causes of this error, and how to fix in each case:
1. Persistent unit name in code does not match the one in JPA config file
In Java code, the persistence unit name is specified like this:
entityManagerFactory = Persistence.createEntityManagerFactory("BookStoreWebsite");
But in the persistence.xml file, it is declared like this:
the persistence.xml file, it is declared like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>
<persistence-unit name="BookstoreWebsite">
<properties>
...
</properties>
</persistence-unit>
</persistence>
Do you see the difference? It is the letter s/S in Bookstore. So check persistence name in both code and XML file carefully - ensure that they are the same.
2. Typos in name of JPA config file
The name of JPA config file must be
persistence.xml, but somehow you name it slightly different, e.g. peristence.xml - a letter s is missing. So check typos in file name to make sure it is persistence.xml exactly.
3. JPA Config file is not in the right location
By default, the persistence.xml file must be present in the classpath, under
src/META-INF folder of the project, as shown below:
So make sure that you put the JPA config file in the right location.
4. Incompatible Hibernate core version
Another cause of the error
No Persistence provider for EntityManager named XXX is the version of Hibernate core dependency in your project. For example, hibernate-core-5.0.12 is known to cause this kind of error, whereas hibernate-core-5.2.12 works fine.So try to use different the version of Hibernate core if you encounter this problem.
5. Wrong Dependency Declaration of Hibernate core
Another reason that causes the error No Persistence provider for Entity Manager is the delcared type and scope of Hibernate core dependency is like this:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Final</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
Remove this kind of type (pom) and scope (compile), and the error will be resolved. Okay, so far I’ve shared with you guys, 4 solutions which you could consider to fix the error
No Persistence provider for EntityManager named XXX. Hope you find this post helpful.
How to Solve other Hibernate/JPA errors:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on
Facebook and watch
his Java videos you YouTube.