Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Fiz igual à aula, porém, meu eclipse não da um update, e sim um novo insert

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&amp;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=?
1 resposta
solução!

Fiz um teste buscando uma conta de ID = 23, deu o merge certinho

Conta conta = em.find(Conta.class, 23L);

    em.close();


    EntityManager em2 = emf.createEntityManager();

    conta.setSaldo(6969);

    em2.getTransaction().begin();

    em2.merge(conta);

    em2.getTransaction().commit();




select
    conta0_.id as id1_2_0_,
    conta0_.agencia as agencia2_2_0_,
    conta0_.numero as numero3_2_0_,
    conta0_.saldo as saldo4_2_0_,
    conta0_.titular as titular5_2_0_,
    movimentac1_.conta_id as conta_id6_3_1_,
    movimentac1_.id as id1_3_1_,
    movimentac1_.id as id1_3_2_,
    movimentac1_.conta_id as conta_id6_3_2_,
    movimentac1_.data as data2_3_2_,
    movimentac1_.descricao as descrica3_3_2_,
    movimentac1_.tipoMovimentacao as tipoMovi4_3_2_,
    movimentac1_.valor as valor5_3_2_ 
from
    Conta conta0_ 
left outer join
    Movimentacao movimentac1_ 
        on conta0_.id=movimentac1_.conta_id 
where
    conta0_.id=?

Hibernate: select conta0_.id as id1_2_0_, conta0_.agencia as agencia2_2_0_, conta0_.numero as numero3_2_0_, conta0_.saldo as saldo4_2_0_, conta0_.titular as titular5_2_0_ from Conta conta0_ where conta0_.id=? Hibernate: select movimentac0_.conta_id as conta_id6_3_0_, movimentac0_.id as id1_3_0_, movimentac0_.id as id1_3_1_, movimentac0_.conta_id as conta_id6_3_1_, movimentac0_.data as data2_3_1_, movimentac0_.descricao as descrica3_3_1_, movimentac0_.tipoMovimentacao as tipoMovi4_3_1_, movimentac0_.valor as valor5_3_1_ from Movimentacao movimentac0_ where movimentac0_.conta_id=?