-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.
– Shura16
To save the Loan, you must have a
Transaction
open and realize thecommit()
. Remembering that you will already pass the client in Emprestimopojo when saving and that your method list does not seek the customer loans.– Shura16
It has to put the class Emprestimopojo, there too?
– Sérgio Mucciaccia