I have the class BankDataEntity
that has a relationship ManyToOne
with IbanEntity
@Entity
public class BankDataEntity
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumns(value = { @JoinColumn(name = "fk_rib", referencedColumnName = "rib", updatable = true),
@JoinColumn(name = "fk_iban", referencedColumnName = "iban", updatable = true) })
private IbanEntity ibanEntity;
}
I want to update the BankDataEntity
, so in the below method, i get BankDataEntity
by id, then i set the new value in IbanEntity
, then i save BankDataEntity
.
The problem is that it adds a new IbanEntity in the database instead of modifying the existing one.
public void on(BankDataUpdated event) {
BankDataEntity bankDataEntity = bankDataRepository.findByInternalId(event.internalId);
IbanEntity currentIban = bankDataEntity.getIbanEntity();
IbanEntity newIban = currentIban.iban(event.iban).rib(event.rib);
bankDataRepository.save(bankDataEntity.ibanEntity(newIban));
}