I am having some trouble with orphan removal on one of my Spring Boot entity classes. When saving new data it has no problem updating the table with the data, however if I am saving the same data it will remove it from the table. For example:
If I have no data in the table and update it with following data: [{id: 1, property: "abc"}, {id: 1, property: "def"}, {id: 1, property: "xyz"}] It will save all of that to the table without any problems. If I then try to save [{id: 1, property: "abc"}, {id: 1, property: "def"}] I am expecting it to remove the row with the "xyz" property. However what actually happens is that all of the data will be removed, leaving nothing behind. It's like somehow it is not recognizing the new list coming in and assumes all data is now orphaned so it gets removed. I have tried all different Cascade types and removing orphanRemoval but none of those have had any success in creating the logic I am looking for. An example of the entity code is here:
public class Class {
ClassPK id;
...properties;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumns({
@JoinColumn(name = "ACCT_NB" ,referencedColumnName="ACCT_NB", updatable = false, insertable = false),
@JoinColumn(name = "LINE_NB" ,referencedColumnName="LINE_NB", updatable = false, insertable = false)
})
private List<RelatedClass> relatedClassList;
}
I am saving the list with a simple save and flush as such:
classRepository.saveAllAndFlush(classList)
With the classList being a list of the Class parent where each Class object within the list contains a list of RelatedClass. The relatedClassList is what is getting cleared out when saving. I have no problems with the classList itself.