简体   繁体   English

如何只删除多对多关系中的关系?

[英]How to delete only the relation in a many to many relationship?

I have the following three tables:我有以下三个表:

player (id, name)
status (id, status_text)
player_status (player_id, status_id)

The player_status table combines the first two tables with a n-to-n relationship player_status 表以 n 对 n 的关系组合了前两个表

Table " player ":表“播放器”:

id ID player玩家
agzua76t34gusad agzua76t34gusad "Anna" “安娜”
sdahb433tbjsdbv sdahb433tbjsdbv "Julia" “茱莉亚”

Table " status ":表“状态”:

id ID status_text状态文本
jjbsdnv8677v6df jjbsdnv8677v6df "operational" “操作”
bulsiu783fdszjh bulsiu783fdszjh "violated" “违反”

Table " player_status "表“ player_status

record_id记录编号 record_status_id record_status_id
agzua76t34gusad agzua76t34gusad jjbsdnv8677v6df jjbsdnv8677v6df
sdahb433tbjsdbv sdahb433tbjsdbv bulsiu783fdszjh bulsiu783fdszjh

The player can have a status assigned or not.玩家可以分配或不分配状态。 Now when a player has a status, how can I remove this status, so that the player but also the status stays in the tables but only the relation in the player_status table will be removed.现在,当玩家有状态时,我如何删除此状态,以便玩家和状态保留在表中,但只会删除player_status表中的关系。

These are the classes for Player and Status这些是 Player 和 Status 的类

@Entity
@Table(name = "player")
public class Player {

    @Column(nullable = false)
    private String id;

    @Column(nullable = false)
    private String name;

    @ManyToMany(fetch = FetchType.LAZY,
        cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
        })
    @JoinTable(name = "player_status",
        joinColumns = {@JoinColumn(name = "player_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "status_id", referencedColumnName = "id")})
    private Set<Status> statusList = new HashSet<>();
}


@Entity
@Table(name = "status")
public class Status {

    @Column(nullable = false)
    private String id;

    @Column(name = "status_text", nullable = false)
    private String statusText;

    @ManyToMany(fetch = FetchType.LAZY,
        cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
        }, mappedBy = "statusList")
    @JsonIgnore
    private Set<Player> players = new HashSet<>();
}

This is how the relation table is created in *.sql :这是在*.sql中创建关系表的方式:

create table player_status
(
    player_id varchar references player (id) on update cascade on delete cascade,
    status_id varchar references status (id) on update cascade
);

How can I delete only an entry from the player_status table?如何只删除 player_status 表中的一个条目? I tried to retrieve a player from the db, changed/removed his status, but this did not update the player.我试图从数据库中检索玩家,更改/删除他的状态,但这并没有更新玩家。

You just remove the associated Status from the statusList collection, and the row of the association table will be automatically deleted when the EntityManager is flushed.您只需从statusList集合中移除关联的Status ,当刷新EntityManager时关联表的行将自动删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM