[英]Hibernate: how to delete entities in many-to-many relationships with extra attributes
I have the following relationships:我有以下关系:
@OneToMany(fetch = FetchType.EAGER, mappedBy="note", cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Review> reviews = new HashSet<>();
in Note.java and在Note.java和
@OneToMany(fetch = FetchType.EAGER, mappedBy = "expert", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Review> notes = new HashSet<>();
in Expert.java .在Expert.java 中。
As you can see, they refer to the Review class, which represents a M to N relationship between Note and Expert.如您所见,它们指的是 Review 类,它表示 Note 和 Expert 之间的 M 到 N 关系。 As I needed to add extra attributes to this relationship, I have created a ReviewId class and a Review class as follows:由于我需要为此关系添加额外的属性,因此我创建了一个 ReviewId 类和一个 Review 类,如下所示:
@Embeddable
public class ReviewId implements Serializable {
@Column(name = "fk_note")
protected Long noteId;
@Column(name = "fk_expert")
protected Long expertId;
[...]
}
@Entity
public class Review {
@EmbeddedId
private ReviewId id;
private int value;
private String comment;
@ManyToOne
@JoinColumn(name = "fk_note", insertable = false, updatable = false)
private Note note;
@ManyToOne
@JoinColumn(name = "fk_expert", insertable = false, updatable = false)
private Expert expert;
[...]
}
When I try to delete a Note or an Expert that appear in this relationship, I get the following error:当我尝试删除出现在此关系中的注释或专家时,出现以下错误:
Cannot delete or update a parent row: a foreign key constraint fails (`db_example`.`review`, CONSTRAINT `FKls65s9wl28v98ts2kifir37p7` FOREIGN KEY (`fk_note`) REFERENCES `note` (`id`))
How can I make Hibernate delete all Reviews related to a certain Note/Expert when I delete them?当我删除某个注释/专家时,如何让 Hibernate 删除与它们相关的所有评论? Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.