No Persistence provider for EntityManager named... error

17,226

You will need to move the persistence.xml file to an appropriate location

From JPA spec:

A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit.

The root of the persistence unit is the key here.

If you are a non-Java EE app

The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.

If you are in a Java EE app, the following are valid

In Java EE environments, the root of a persistence unit must be one of the following:

  • an EJB-JAR file
  • the WEB-INF/classes directory of a WAR file[80]
  • a jar file in the WEB-INF/lib directory of a WAR file
  • a jar file in the EAR library directory
  • an application client jar file
Share:
17,226
ebruszl
Author by

ebruszl

Updated on June 04, 2022

Comments

  • ebruszl
    ebruszl almost 2 years

    My persistence xml file is like that

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0">
        <persistence-unit name="hibernateEbru">     
            <provider>org.hibernate.ejb.HibernatePersistence</provider>     
            <class>com.hibernate.business_card</class> 
            <properties>            
                <property name="hibernate.hbm2ddl.auto" value="create" />           
                <property name="hibernate.show_sql" value="true" />         
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />         
                <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />         
                <property name="hibernate.connection.username" value="root" />          
                <property name="hibernate.connection.password" value="2643" />          
                <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/BusinessDb" /> 
    
            </properties>
        </persistence-unit>
    </persistence>
    

    Then I have my code calling it with this:

    public class test {
        public static void main(String[] args) {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("hibernateEbru");
    
            EntityManager em = emf.createEntityManager();
    
            em.getTransaction().begin();
    
            business_card bc = new business_card();
            bc.setName("Ebru");
    
            em.persist(bc);
    
            em.getTransaction().commit();
    
            em.close();
    
            emf.close();
    
        }
    }
    

    I got the following error message:

    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named hibernateEbru
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
        at com.hibernate.test.main(test.java:8)