I have a problem with the following many to many relationship
Two entities Person and Group
mapping in Person
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "person_group",
joinColumns = {@JoinColumn(name = "person_id")},
inverseJoinColumns = {@JoinColumn(name = "group_id")})
private Set<Group> groups = new HashSet<>();
mapping in Group
@ManyToMany(mappedBy = "groups" , fetch = FetchType.LAZY, cascade = {CascadeType.MERGE})
@JsonIgnore
public Set<Person> getPeople() {
if (people == null){
people = new HashSet<>();
}
return people;
}
We have a spring batch job for inserting, updating People
At the start we use raw sql to persist all the potential groups as this is predefined reference data.
For each Person we get a group by id and associate the Person and Group on both directions.
At the end of the batch job we save and flush the list of People.
The problem is we are ending up with duplicate entries in the Group table.
Any help would be greatly appreciated.
Thanks