0

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));
}
Aymen Kanzari
  • 1,765
  • 7
  • 41
  • 73
  • Does this answer your question? [JPA EntityManager: Why use persist() over merge()?](https://stackoverflow.com/questions/1069992/jpa-entitymanager-why-use-persist-over-merge) – K.Nicholas Nov 03 '20 at 14:21
  • @K.Nicholas Is this a dupe? OP is using Spring repository which is not the same as using EntityManager directly. – pirho Nov 03 '20 at 16:11
  • Should lead him in the right direction. This question has been asked and answered so many times here on SO. – K.Nicholas Nov 03 '20 at 18:26

0 Answers0