Caused by: jakarta.persistence.PersistenceException: No Persistence provider for EntityManager




Asked on February 02, 2023
In my Hibernate application, I am using persistence.xml as

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="3.0">
<persistence-unit name="com.myproject">
<description>My Project</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.myproject.entity.User</class>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect" />
<property name="jakarta.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="jakarta.persistence.jdbc.url"
value="jdbc:mysql://localhost/myproject" />
<property name="jakarta.persistence.jdbc.user" value="root" />
<property name="jakarta.persistence.jdbc.password"
value="abcd" />
</properties>
</persistence-unit>
</persistence>

When I run the application, I am getting the error.

Caused by: jakarta.persistence.PersistenceException: No Persistence provider for EntityManager named com.myproject
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:86)
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at com.concretepage.util.HibernateUtil.<clinit>(HibernateUtil.java:11)



Replied on February 02, 2023
Check for following points.

1. The location of persistence.xml should be <classpath>/META-INF/persistence.xml

2. Hibernate dependencies should be resolved.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.1.6.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.6.14.Final</version>
</dependency>

3. In the Hibernate version 4.3 and above, org.hibernate.ejb.HibernatePersistence has been replaced by org.hibernate.jpa.HibernatePersistenceProvider .
Use following line in persistence.xml

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

Reference:
https://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/ejb/HibernatePersistence.html




Replied on February 02, 2023
Thanks. Issue is resolved. I replaced org.hibernate.ejb.HibernatePersistence by org.hibernate.jpa.HibernatePersistenceProvider and it worked.

Write Answer








©2024 concretepage.com | Privacy Policy | Contact Us