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;
}
}