'JPA/Hibernate ManyToMany trying to get data to persist
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
