JPA/Hibernate ManyToMany 尝试让数据持久化
我有一个任务,我们有一个电视/体育应用程序,用户可以在其中提交赛事、球队和赛事所在的电视频道。我希望如果我删除一个频道,它会更新数据库中的其他事件以删除该频道,但我希望该事件持续存在。
我的讲座告诉我们使用双向多对多,但它有同样的问题,当我删除一个通道时,它会删除具有该通道的所有事件,然后还会删除参与该事件的所有团队。这是可以用下面的代码完成的事情还是我以错误的方式进行?
我使用了通道表中的每个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论