[英]Spring Data JPA | Remove only entries when removing MyGroup from @JoinTable in @ManyToMany Relationship
I am using spring Boot 2.0 and spring data jpa with hibernate. I am creating a web application where a user can make group, and other user can join this group.我正在使用 spring Boot 2.0 和 spring 数据 jpa 和 hibernate。我正在创建一个 web 应用程序,用户可以在其中创建组,其他用户可以加入该组。 For this, i created two Entity named User and MyGroup as below.
为此,我创建了两个名为 User 和 MyGroup 的实体,如下所示。
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
@Entity
public class User extends BaseEntity {
String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "members")
Set<MyGroup> joinedGroup= new HashSet<MyGroup>();
}
@Entity
public class MyGroup extends BaseEntity {
String name;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
Set<User> members = new HashSet<User>();
}
Here, I want that, If a group is removed, then it's relationship entry from joined table is also removed but user should not be removed.在这里,我想要的是,如果删除了一个组,那么它的连接表中的关系条目也将被删除,但不应删除用户。 This code is removing even users also.
此代码甚至还删除了用户。 plz suggest how may i delete group and relationship entry only.
请建议我如何只删除组和关系条目。
CascadeType.ALL at MyGroup.java removes all the associated users upon deleting a group. MyGroup.java 中的 CascadeType.ALL 在删除组时删除所有关联的用户。 So, replace the cascade CascadeType.ALL with CascadeType.MERGE
因此,将级联 CascadeType.ALL 替换为 CascadeType.MERGE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.