JPA EclipseLink - Does not update entity

-1

I have a relationship in Client JPA 1 - > 0 .. * Loans. So I have in the client class a List Loan set to do OneToMany. It turns out that when I add a client it updates the normal client list, however when I add a loan it does not update the List loans I have inside clients. Only after restarting tomcat does it update the Loans list.

I have to add the direct loan in the List and give a persiste in the client?

At the moment I'm giving a direct persistence in the loan class.

@Entity (name="cliente")
public class ClientePOJO {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String nome;
    private String sobrenome;
    private String rg;
    private String cpf;
    @Transient
    private String nascimentoString;
    @Temporal(TemporalType.TIMESTAMP)
    private Date nascimento;
    private String telefoneResidencial;
    private String telefoneCelular;
    private String endereco;
    @Transient
    private String icon = "ui-icon-blank";


    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="cliente")
    @JoinColumn(name="cliente_id")
    private List<EmprestimoPOJO> emprestimos;

Client Persistence:

public static boolean adiciona(ClientePOJO novo){
        try{
            factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
            EntityManager em = factory.createEntityManager();
            em.getTransaction().begin();
            em.persist(novo);
            em.getTransaction().commit();
            System.out.println("Dados Gravados com Sucesso");
            return true;
        }
        catch (Exception e){
            System.out.println(e);
            System.out.println("Erro ao gravar no banco de dados");
            return false;
        }
    }

public static List<ClienteBean> selectAll(){
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();
    Query query = em.createQuery("select c from cliente c");
    return query.getResultList();

}

Loan Persistence:

public static boolean adiciona(EmprestimoPOJO novo){
        try{
            factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
            EntityManager em = factory.createEntityManager();
            //EntityTransaction tx = em.getTransaction();
            em.getTransaction().begin();

            //tx.begin();
            em.persist(novo);
            //tx.commit();
            em.getTransaction().commit();
            System.out.println("Dados Gravados com Sucesso");
            return true;
        }
        catch (Exception e){
            System.out.println(e);
            System.out.println("Erro ao gravar no banco de dados");
            return false;
        }
    }
    
asked by anonymous 19.06.2016 / 21:21

1 answer

0

Initially you must have the Loan already registered in the bank to add to the client.

If you register on time, together with the customer, you must give a persist() in the loan register and only then in the Customer's registry.

If you save the client first, register the Loan and only then associate the Loan with the Client, then it is a merge() in the Customer's registry.

Remembering to commit (with commit() ) your transactions at the end of the process before consulting again.

    
19.06.2016 / 21:28