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.