JPA / Hibernate - 删除子项删除父项(从同一个表)

问题描述 投票:1回答:1

我有一个类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>();
}

干杯

java hibernate jpa many-to-one hibernate-onetomany
1个回答
0
投票

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>();
}
© www.soinside.com 2019 - 2024. All rights reserved.