No Persistence provider for EntityManager named

1

I've been studying hibernate framework with JPA in a simple example of face I'm already having problems and my searches have not got me anywhere to solve my problem.

This is the error you are giving:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testePU
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.exemplo.repositorio.RepositorioCliente.<init>(RepositorioCliente.java:18)
    at com.exemplo.testes.Teste.main(Teste.java:10)

My persistence file looks like this:

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

<persistence version="2.0"
    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_2_0.xsd">

    <persistence-unit name="testePU" transaction-type="RESOURCE_LOCAL">
    </persistence-unit>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <properties>
            <property name="hibernate.archive.autodetection" value="class" />

            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/loja"/>
            <property name="hibernate.connection.user" value="root"/>
            <property name="hibernate.connection.password" value="lu121190"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

            <!-- validate | update | create | create-drop -->
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLSInnoDBDialect"/>
        </properties>

</persistence>

I have a class for repository that looks like this:

public class RepositorioCliente {

    EntityManagerFactory emf;
    EntityManager em;

    public RepositorioCliente() {
        emf = Persistence.createEntityManagerFactory("testePU");
        em = emf.createEntityManager();
    }

    public void salvar(Cliente c) {
        em.getTransaction().begin();

        em.merge(c);
        em.getTransaction().commit();
        emf.close();
    }

    public void remover(Cliente c) {
        em.getTransaction().begin();

        em.remove(c);
        em.getTransaction().commit();
        emf.close();
    }

    @SuppressWarnings("unchecked")
    public List<Cliente> listarTodos(){
        em.getTransaction().begin();

        Query consulta = em.createQuery("select cliente from Cliente cliente");
        List<Cliente> clientes = consulta.getResultList();
        em.getTransaction().commit();
        emf.close();
        return clientes;
    }

My Client Class:

@Entity
@Table(schema="loja", name="cliente")
public class Cliente {

    @Column(name= "codigo", nullable= false, unique= true, updatable= false)
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long codigo;
    @Column
    private String nome;
    @Column
    private Integer idade;
    @Column
    private char sexo;
    @Column
    private String profissao;
    public Long getCodigo() {
        return codigo;
    }
    public void setCodigo(Long codigo) {
        this.codigo = codigo;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public Integer getIdade() {
        return idade;
    }
    public void setIdade(Integer idade) {
        this.idade = idade;
    }
    public char getSexo() {
        return sexo;
    }
    public void setSexo(char sexo) {
        this.sexo = sexo;
    }
    public String getProfissao() {
        return profissao;
    }
    public void setProfissao(String profissao) {
        this.profissao = profissao;
    }
}

And finally my test class with the main method:

public class Teste {

    public static void main(String[] args) {

        RepositorioCliente repositorioCliente = new RepositorioCliente();

        Cliente cliente = new Cliente();
        cliente.setNome("João da Silva");
        cliente.setIdade(30);
        cliente.setSexo('M');
        cliente.setProfissao("Engenheiro");

        repositorioCliente.salvar(cliente);
    }

}

If someone can help me from now on thanks.

    
asked by anonymous 03.09.2018 / 04:45

1 answer

0

Some possibilities to avoid this error:

  • Check that the persistence.xml file is in the <webroot>/WEB-INF/classes/META-INF
  • Within <persistence-unit name="testePU" transaction-type="RESOURCE_LOCAL"> , add:

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    
  • Make sure you have the correct dependencies in your classpath:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.3.6.Final</version>
    </dependency>
    

    If you are not using Maven, configure whether you have the hibernate-core.jar file in the application's classpath. The hibernate-entitymanager.jar file is deprecated and may cause this error as well.

05.09.2018 / 20:42