映射:
@Entity
@Table(name = "LABEL")
public class Label extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LABEL_ID")
private Integer labelId;
@ManyToMany(mappedBy = "labels")
private Set<Group> groups = new HashSet<>();
}
@Entity
@Table(name = "GROUP")
public class Group extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "GROUP_ID")
private Integer groupId;
@ManyToMany()
@JoinTable(name = "GROUP_LABEL", joinColumns = {
@JoinColumn(name = "GROUP_ID")},
inverseJoinColumns = {@JoinColumn(name = "LABEL_ID")
})
private Set<Label> labels = new HashSet<>();
}
因此创建了3个表=> LABEL,GROUP和GROUP_LABEL。
我想应用级联来像这样工作:
如果我删除ID为ex的元素。从GROUP表15中删除我希望删除GROUP_LABEL中ID为15的所有记录,并也从LABEL中将它们与它们的标签相关联。
如果我删除ID为ex的元素。从LABEL表中删除10我希望删除GROUP_LABEL中ID为10的所有记录,但不应删除与它们相关的组。