I have already looked into this issue that I am having and tried to remove the AccountBalance from the Set in the corresponding Account Hibernate class, and then saving the Account object, but the database isn't updated.
I want to delete an AccountBalance record from the database using Hibernate. An Account can have many AccountBalances, but an AccountBalance can only have one Account.
I am using a repository class which extends CrudRepository
to save Hibernate instances to their corresponding tables in the database. I have tried using the delete()
function specifically on the AccountBalance I want to delet, but that doesn't work.
Here is the Hibernate code I already have in AccountBalance:
@Entity
@Table(name = "account_balance")
public class AccountBalance {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "acc_id", nullable = false)
private Account account;
And Account:
@Entity
@Table(name = "account")
public final class Account {
@OneToMany(fetch = FetchType.EAGER, mappedBy = "account", cascade = CascadeType.ALL)
private Set<AccountBalance> balances = new HashSet<>();
I have already tried this functionality to no avail.
public void deleteAccountBalance(Account account, String balanceCode) {
for (Iterator<AccountBalance> iterator = account.getBalances().iterator(); iterator.hasNext();) {
AccountBalance accBal = iterator.next();
if (accBal.getBalanceCode().equals(balanceCode)) {
iterator.remove();
}
}
accountRepository.save(account);
}
Any ideas on what I am doing wrong?