我有一个类Comment(见下文),其中一些Comment对象属于父Comment。到目前为止,当我删除父注释时,子项也被删除(如预期的那样),但是当删除子项时会出现问题,因为父项也被删除了。我想问题来自于类中使用的JPA配置。任何想法如何删除子而不影响父行?
public class Comment {
@Column
private String text;
@ManyToOne(cascade={CascadeType.ALL})
private Comment parent;
@OneToMany(cascade={CascadeType.ALL}, mappedBy="parent")
private Set<Comment> childs = new HashSet<Comment>();
}
干杯
从cascade={CascadeType.ALL}
的映射中删除parent
:
public class Comment {
@Column
private String text;
@ManyToOne
private Comment parent;
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent") // or orphanRemoval=true
private Set<Comment> childs = new HashSet<Comment>();
}