I have a relation n-n mapped with these 3 entities :
@Entity
public class ApplicatifDo {
.....
@OneToMany(cascade = CascadeType.ALL, mappedBy = "applicatifDo", fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ApplicatifTerminalDo> applicatifTerminalSet;
.....
}
@Entity
public class ApplicatifTerminalDo {
......
@ManyToOne
@JoinColumn(name = "idApplicatif", nullable = false)
private ApplicatifDo applicatifDo;
@ManyToOne
@JoinColumn(name = "idTerminal", nullable = false)
private TerminalDo terminalDo;
@Column
private String remarques;
......
}
@Entity
public class TerminalDo {
......
@OneToMany(cascade = CascadeType.ALL, mappedBy = "terminalDo", fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ApplicatifTerminalDo> applicatifTerminalSet;
......
}
I try to do a test where I have an ApplicatifDo persisted which contains many ApplicatifTerminalDo in its Set, and I change some values of these, remove others, add new ones in this Set :
modifyRemarques(firstElement(applicatifDo.getApplicatifTerminalSet()));
applicatifDo.getApplicatifTerminalSet.add(anotherApplicatifTerminal);
applicatifDo.getApplicatifTerminalSet.remove(onceApplicatifTerminal);
Then when I merge that in my DB, in my join-table ApplicatifTerminal I see the modification of "Remarques" appearing, as well as the new ApplicatifTerminal entities appearing, but the remove doesn't work, and the entities that should be deleted are still present in my join-table (and I use orphanRemoval).
And I get no error in the console when I test that, but still the remove entities shouldn't be in DB anymore.
PS : I used the same method on a 1-n relation and the cascading of all types of modification on a contained Set of entities in a parent entity were working well (remove included).
Update :
I tried to remove the onceApplicatifTerminal from the TerminalDo as said by Chris, but still doesn't work. Here is the code :
Iterator<ApplicatifTerminalDo> iterator = applicatifDo
.getApplicatifTerminalSet().iterator();
boolean first = true;
while (iterator.hasNext()) {
ApplicatifTerminalDo element = iterator.next();
if (!first) {
element.setRemarques("remarques updated");
} else {
iterator.remove();
element.getTerminalDo().setApplicatifTerminalSet(
applicatifDo.getApplicatifTerminalSet());
first = false;
}
}
Here I try to remove the first ApplicatifTerminalDo from the list and update a column value for others. And when I merge, the values are changed but the remove doesn't work, even if I get the TerminalDo to remove put the updated ApplicatfTerminal Set.
Is it possible to achieve what I want to do ?
What is the best practice for updating/removing rows from a personnally-made join table ?
Please help, i don't understand why.