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:
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:
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.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments
pom-file:
org.hibernate
hibernate-core
6.1.6.Final
Path from project repository root to persistence.xml
src/main/resources/META-INF/persistence.xml
org.hibernate.jpa.HibernatePersistenceProvider
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("request");