Hi,
I have two entity classes. ClientProfile.java and ClientOtherLoan.java.
They are related with OneToMany.
ClientProfle:
Code:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "client", orphanRemoval = true)
@Fetch (FetchMode.SELECT)
private List<ClientOtherLoan> otherLoans;
ClientOtherLoan:
Code:
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "client_id", nullable = false)
@ForeignKey(name = "fk_client_other_loan")
private ClientProfile client;
When I insert or update client profile everything works fine - client other loans are also inserted/updated.
But when I want to delete some of the loans (removing them from otherLoans list and calling update() on client), they won't disappear from DB.
They still exist.
Before calling update() on ClientProfile object I will loop through otherLoans and remove those that are empty:
Code:
private ClientProfile manageOtherLoans(ClientProfile client) throws ControllerException {
if (client.getOtherLoans() != null) {
List<ClientOtherLoan> list = new ArrayList<ClientOtherLoan>();
for (ClientOtherLoan loan : client.getOtherLoans()) {
if (loan.getAmount() != null && loan.getPayback() != null) {
loan.setClient(client);
list.add(loan);
}
}
client.setOtherLoans(list);
}
return client;
}
I was expecting that if hibernate removes all loans it doesn't find from otherLoans list - but it doesn't.
Can anyone help and say what am I doing wrong?
Many thanks,
Risto