Affects ORM 6.2, but not 5.6, and (I think) not 6.1 either. With the following model, attempting to remove one entity from the associations may result in the whole association being cleared.
@Entity(name = "containing")
public static class ContainingEntity {
@Id
private Integer id;
@ManyToMany
@JoinTable(inverseJoinColumns = @JoinColumn(name = "inverse"))
@SortNatural
private SortedSet<ContainedEntity> contained = new TreeSet<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public SortedSet<ContainedEntity> getContained() {
return contained;
}
public void setContained(SortedSet<ContainedEntity> contained) {
this.contained = contained;
}
}
@Entity(name = "contained")
public static class ContainedEntity implements Comparable<ContainedEntity> {
@Id
private Integer id;
@ManyToMany(mappedBy = "contained")
private List<ContainingEntity> containing = new ArrayList<>();
@Override
public int compareTo(ContainedEntity o) {
return getId() - o.getId();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<ContainingEntity> getContaining() {
return containing;
}
}
The problem appears to be in the SQL generated to clear the association, which completely ignores one side of the association:
Whereas, when @JoinTable(inverseColumn) is not used, we get:
Removing @JoinTable(inverseColumn = ...), or moving from SortedSet to List, makes the problem disappear. I will submit a reproducer in the comments. |