Persistence.xml
<persistence 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"
version="2.0">
<persistence-unit name="contas">
<provider> org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>br.com.cskmacedo.jpa.modelo.Conta</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/alura_jpa?useTimezone=true&serverTimezone=UTC "/>
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="Dev2020@" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Conta.java
package br.com.cskmacedo.jpa.modelo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Conta {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer agencia;
private Integer numero;
private String titular;
private Double saldo;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getAgencia() {
return agencia;
}
public void setAgencia(Integer agencia) {
this.agencia = agencia;
}
public Integer getNumero() {
return numero;
}
public void setNumero(Integer numero) {
this.numero = numero;
}
public String getTitular() {
return titular;
}
public void setTitular(String titular) {
this.titular = titular;
}
public Double getSaldo() {
return saldo;
}
public void setSaldo(Double saldo) {
this.saldo = saldo;
}
}
CriarContaComSaldo.java
package br.com.cskmacedo.jpa.modelo;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class CriaContaComSaldo {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("contas");
EntityManager em = emf.createEntityManager();
Conta conta = new Conta();
conta.setTitular("Márcia");
conta.setNumero(709038);
conta.setAgencia(1228);
conta.setSaldo(100.0);
em.getTransaction().begin();
em.persist(conta);
em.getTransaction().commit();
em.close();
EntityManager em2 = emf.createEntityManager();
System.out.println("ID da conta da Márcia: " + conta.getId());
conta.setSaldo(500.0);
em2.getTransaction().begin();
em2.merge(conta);
em2.getTransaction().commit();
}
}
O eclipse retorna o codigo com o update igual ao eclipse do instrutor, porém, em meu banco, ao invés de atualizar o valor da conta da Márcia, ele insere novamente o registro com o novo valor.
id, agencia, numero, titular, saldo '1', '1228', '709036', 'Kleber', '20' '2', '1228', '709037', 'Ronaldo', '500' '3', '1228', '709037', 'Ronaldo', '1000' '4', '1228', '709038', 'Reinaldo', '100' '5', '1228', '709038', 'Márcia', '100' '6', '1228', '709038', 'Márcia', '500'
Abaixo, a resposta do Console do eclipse:
Hibernate:
insert
into
Conta
(agencia, numero, saldo, titular)
values
(?, ?, ?, ?)
ID da conta da Márcia: 6
Hibernate:
select
conta0_.id as id1_0_0_,
conta0_.agencia as agencia2_0_0_,
conta0_.numero as numero3_0_0_,
conta0_.saldo as saldo4_0_0_,
conta0_.titular as titular5_0_0_
from
Conta conta0_
where
conta0_.id=?
Hibernate:
update
Conta
set
agencia=?,
numero=?,
saldo=?,
titular=?
where
id=?