JPA Eclipselink - No entity update

Asked

Viewed 181 times

-1

I have a relationship in the JPA of Client 1 -> 0.. * Loans. So I have in the client class a set Loan List to make the Onetomany. It turns out that when I add a customer he updates the normal customer list, however when I add a loan he doesn’t update the List of loans I have within clients. Only after restarting Tomcat does it update the Loan list.

I have to add the direct loan to the List and give a persist in the customer?

At the moment I’m giving a straight persist on 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;

Persistencia Cliente:

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();

}

Persistencia Emprestimo:

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;
        }
    }
  • To help, post the method you use to save.

  • To save the Loan, you must have a Transaction open and realize the commit(). Remembering that you will already pass the client in Emprestimopojo when saving and that your method list does not seek the customer loans.

  • It has to put the class Emprestimopojo, there too?

1 answer

0

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

If you register on time, together with the client, you must give a persist() in the Loan registration and only then in the Client’s registration.

If you save the client first, register the Loan and only then associate the Loan to the Client, then it is a merge() in the Client’s register.

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

  • I am doing in the following order. I register the loan. And then, when accessing another menu, it perform a new selectAll to renew the customer list I have.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.