JPA/Hibernate ManyToMany 尝试让数据持久化

发布于 2025-01-10 01:26:14 字数 2737 浏览 0 评论 0原文

我有一个任务,我们有一个电视/体育应用程序,用户可以在其中提交赛事、球队和赛事所在的电视频道。我希望如果我删除一个频道,它会更新数据库中的其他事件以删除该频道,但我希望该事件持续存在。

我的讲座告诉我们使用双向多对多,但它有同样的问题,当我删除一个通道时,它会删除具有该通道的所有事件,然后还会删除参与该事件的所有团队。这是可以用下面的代码完成的事情还是我以错误的方式进行?

我使用了通道表中的每个 CascadeType 来查看数据是否会持久存在,但它要么收到 SQL 错误:23503,要么删除通道链接到的所有内容。

事件类

@Entity
@Table(name = "events")
public class Events {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "event_name")
    private String eventName;

    @Column(name= "event_date")
    private Date eventDate;

    @ManyToMany(cascade = {
                    CascadeType.PERSIST,
                    CascadeType.MERGE
            })
    @JoinTable(name = "events_channels",
            joinColumns = { @JoinColumn(name = "events_id") },
            inverseJoinColumns = { @JoinColumn(name = "channels_id") })
    private List<Channels> channels;

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    private List<Teams> homeTeam;

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    private List<Teams> awayTeam;

    public Events() {}
}

通道类

@Entity
@Table(name="channels")
public class Channels {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "channel_name")
    private String channelName;

    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                    CascadeType.REMOVE
            },
            mappedBy = "channels")
    @JsonIgnore
    private List<Events> events;

    public Channels() {}
}

I 也有一个 data.sql,我将以下虚拟数据放入 ManyToMany 连接表中。

INSERT INTO events_channels (events_id, channels_id) VALUES (1, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (1, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (2, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (2, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (3, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (4, 2);
INSERT INTO events_channels (events_id, channels_id) VALUES (5, 2);
INSERT INTO events_channels (events_id, channels_id) VALUES (4, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (5, 4);
INSERT INTO events_channels (events_id, channels_id) VALUES (6, 2);
INSERT INTO events_channels (events_id, channels_id) VALUES (7, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (7, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (8, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (8, 3);

I have an assignment where we have a TV/Sports app where users can submit the event, the teams and the TV channels the event is on. I want it where if I delete a channel it updates the other events in the database to delete that channel too, but I want the event to persist.

My lecture told us to use a bi-directional ManyToMany but its having the same issue where when I delete a channel it deletes all events that have that channel, then also deletes all the teams that are partaking in the event to. Is this something that can be done with the below code or am I going about it in the wrong way?

I used every CascadeType in the channels table to see if the data would persist, but it either gets an SQL Error: 23503 or deletes everything the channel is linked to.

Events Class

@Entity
@Table(name = "events")
public class Events {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "event_name")
    private String eventName;

    @Column(name= "event_date")
    private Date eventDate;

    @ManyToMany(cascade = {
                    CascadeType.PERSIST,
                    CascadeType.MERGE
            })
    @JoinTable(name = "events_channels",
            joinColumns = { @JoinColumn(name = "events_id") },
            inverseJoinColumns = { @JoinColumn(name = "channels_id") })
    private List<Channels> channels;

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    private List<Teams> homeTeam;

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    private List<Teams> awayTeam;

    public Events() {}
}

Channels Class

@Entity
@Table(name="channels")
public class Channels {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "channel_name")
    private String channelName;

    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                    CascadeType.REMOVE
            },
            mappedBy = "channels")
    @JsonIgnore
    private List<Events> events;

    public Channels() {}
}

I also have a data.sql where I put the following dummy data into the ManyToMany join table.

INSERT INTO events_channels (events_id, channels_id) VALUES (1, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (1, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (2, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (2, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (3, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (4, 2);
INSERT INTO events_channels (events_id, channels_id) VALUES (5, 2);
INSERT INTO events_channels (events_id, channels_id) VALUES (4, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (5, 4);
INSERT INTO events_channels (events_id, channels_id) VALUES (6, 2);
INSERT INTO events_channels (events_id, channels_id) VALUES (7, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (7, 3);
INSERT INTO events_channels (events_id, channels_id) VALUES (8, 1);
INSERT INTO events_channels (events_id, channels_id) VALUES (8, 3);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文