No Persistence Provider for Entitymanager named

Asked

Viewed 536 times

1

Guys I’m studying about the Ibernate framework with JPA in a simple example of guy I’m already having problems and my research did not take me anywhere to solve my problem.

that’s the mistake you’re making:

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 is 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 repository class 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 class Client:

@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 lastly 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 anyone can help me I’d appreciate it.

1 answer

0

Some possibilities to avoid this error:

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

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    
  • Make sure you have the right 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 if you have the file hibernate-core.jar in the application’s classpath. The file hibernate-entitymanager.jar is depreciated and may cause this error as well.

Browser other questions tagged

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