简体   繁体   English

Spring 数据 JPA | 从 @ManyToMany 关系中的 @JoinTable 中删除 MyGroup 时仅删除条目

[英]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.

相关问题 Spring JPA并从@ManyToMany中删除条目 - Spring JPA and remove entries from @ManyToMany JPA - @ManyToMany 双向关系,删除反向侧的实体和 JoinTable 中剩余关系的错误 - JPA - @ManyToMany bidirectional relationship, removing Entity on Inverse Side and error with remaining relationship in JoinTable JPA(与Hibernate)@ManyToMany @JoinTable关系级联 - JPA (with Hibernate) @ManyToMany @JoinTable relationship cascade 执行查询时多对多关系不起作用(Spring Data JPA) - ManyToMany relationship not working when executing query (Spring Data JPA) Spring Data Jpa项目使用ManyToMany关系时的生成查询 - Generation query when the ManyToMany relationship is used by Spring Data Jpa project 用于ManyToMany单向关系的Spring Data JPA规范 - Spring Data JPA Specification for a ManyToMany Unidirectional Relationship Spring JPA中的“ ManyToMany”关系问题 - Issue with “ManyToMany” relationship Spring JPA Spring Jpa数据存储库使用LinkedEntity for ManyToMany关系进行保存(更新) - Spring Jpa Data Repository save (update) with LinkedEntity for ManyToMany relationship Spring-Data-JPA ManyToMany与额外列的关系 - Spring-Data-JPA ManyToMany relationship with extra column Spring 数据 JPA 和 hibernate 分离的实体传递以保持多对多关系 - Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM