In my application, I have a tender entity.
@Entity
@Table(name = "tender")
public class Tender {
@Id
@Column(name = "tender_id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@OneToMany(mappedBy="tender", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnoreProperties("tender")
private List<TenderAddress> addresses;
// constructors and getters and setters
// I am showing the setter for Address since it is customized for my requirement
public void setAddresses(List<TenderAddress> addresses) {
this.addresses = addresses;
for (TenderAddress tenderAddress : addresses) {
tenderAddress.setTender(this);
}
}
}
My TenderAddress
entity looks like this.
@Entity
@Table(name = "tender_address")
public class TenderAddress {
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="tender_id")
@JsonIgnoreProperties("addresses")
private Tender tender;
@Column(name = "address", nullable = false)
private String address;
// constructors
//getters and setters
}
Everything worked fine until I started testing the update operation.
This is my update method.
@Transactional
public Tender updateTender(Tender tender) {
Tender t = entityManager.find(Tender.class, tender.getId());
t.setName(tender.getName());
t.setDescription(tender.getDescription());
t.setAddresses(tender.getAddresses());
entityManager.merge(t);
entityManager.flush();
return findById(t.getId());
}
name and description are updated fine. What I thought how the addresses would get updated is, it will edit the current record in the relevant database table. But what happened was, it inserted a new record into the table with out editing the already existing record. In fact, I thought JPA looks after that it self.
So, my question now is, do I have to merge the address table explicitly when merging the tender table? If so, how it should be done?