当前位置: 代码迷 >> java >> JPA /休眠-删除子项会删除父项(从同一表中)
  详细解决方案

JPA /休眠-删除子项会删除父项(从同一表中)

热度:22   发布时间:2023-07-26 14:22:09.0

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

干杯

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>();
}
  相关解决方案