Error : "No Persistence Provider for Entitymanager"

Asked

Viewed 382 times

0

I have a project in Eclipse with an application that persists in the Postgresql 9.6 database through Hibernate, but Java is not recognizing the "persistence-Unit name" which is set in the persistence.xml, even this name conferring with the name passed to the object "Entitymanagerfactory".

Below is the Exception log:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named bd2_persistence_unit
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at br.com.bd2.teste.TesteDatabase.main(TesteDatabase.java:15)

This is the code of my main:

package br.com.bd2.teste;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import br.com.bd2.exemplo.model.Fruteira;

public class TesteDatabase {
    public static void main(String[] args) {
        // Inicializacao da fabrica de objetos
        // persistenceUnitName = "bd2", ou seja, relaciona com a conexao na
        // persistence.xml (podera ter "n" unidades de persistencia)
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("bd2_persistence_unit");
        EntityManager em = emf.createEntityManager();

        // Os comandos a seguir devem ser executados "um por vez"
        // Inserindo...
        em.getTransaction().begin();
        em.persist(new Fruteira("Fruteira"));
        em.persist(new Fruteira("Fruteira 3"));
        em.getTransaction().commit();

        // Atualizando...
        // Buscara a fruteira com id = 1. Observe o numero do id pelo pgAdmin3!
        Fruteira fruteira = em.find(Fruteira.class, 1L); 
        if (fruteira != null) {
            em.getTransaction().begin();
            fruteira.setNome("Fruteira Legal");
            em.merge(fruteira);
            em.getTransaction().commit();
        }
        // Recuperando "n" objetos...
        TypedQuery<Fruteira> q = em.createQuery("SELECT f " +
                                                "FROM Fruteira f", Fruteira.class);
        for (Fruteira each : q.getResultList()) {
            System.out.println(each.toString());
        }       

        // Excluindo...
        // Buscara a fruteira com id = 1. Observe o numero do id pelo pgAdmin3!
        fruteira = em.find(Fruteira.class, 1L); 
        if (fruteira != null) {
            em.getTransaction().begin();
            em.remove(fruteira);
            em.getTransaction().commit();
        }

        // Recuperando "n" objetos...
        q = em.createQuery("FROM Fruteira f", Fruteira.class);
        for (Fruteira each : q.getResultList()) {
            System.out.println(each.toString());
        }

        System.exit(0);
    }
}

This is the organization of my project:

inserir a descrição da imagem aqui

This is my Hibernate configuration xml:

     <?xml version="1.0" encoding="UTF-8"?>

    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
        version="2.1">


        <persistence-unit name="bd2_persistence_unit">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 


            <class>br.com.bd2.exemplo.model.Fruteira</class>
            <exclude-unlisted-classes>true</exclude-unlisted-classes>

            <properties>
                <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

                <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/bd2" />
                <property name="hibernate.connection.username" value="postgres" />
                <property name="hibernate.connection.password" value="postgres" />
                <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.format_sql" value="false" />          

                <property name="hibernate.archive.autodetection" value="true" />
            </properties>
        </persistence-unit>

    </persistence>
  • 1

    Maybe the tag provider in persistence.xml? <Provider>org.hibernate.ejb.Hibernatepersistence</Provider>

  • @Israelmerljak I did it and the problem persists. :(

  • 1

    Amigo.. the Provider tag is inside the persistence-Unit. Depending on the version of Hibernate tbm can org.hibernate.jpa.HibernatePersistenceProvider.

  • @Israelmerljak Corrected, did not help. The name of the persistence unit can be anyone, right? It doesn’t have to have to do with the name of my bank that’s running on Postgree?

  • 2

    Yeah.. PU name can be whatever you want it to be. as long as the url is pointing to the right base.. dude.. take a look at this one question in the Alura forum. It’s a very similar problem to yours. It was suggested to check the libraries in the project. According to the answer there, it seems to me that Hibernate-entitymanager is missing in your classpath.

  • Okay, I’ll check it out.

  • I put the Entity Manager in the classpath and the error continues...

Show 2 more comments

1 answer

-1

In your persistence.xml, or file you configure the persistence of Hibernate has a tag there has a name attribute.

These Names must be the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.