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.