问题描述
我有一个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>();
}
干杯
1楼
从parent
cascade={CascadeType.ALL}
映射中删除cascade={CascadeType.ALL}
:
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>();
}