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:



persistence xml meta-inf

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:

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.



Add comment

   


Comments 

#4Thashwin C S2023-09-22 14:00
Still I'm getting same error
Quote
#3Edward2023-01-29 18:41
Great, that's professionalism... Even character escaping isn't set up for comments. My comment has become completely useless and I can't even fix it.
Quote
#2Edward2023-01-29 18:37
It Didn't help((

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");
Quote
#1omid azadi2022-08-10 06:11
You just saved my day sir. Cheers!
Quote