title: JPA/Hibernate ManyToMany trying to get data to persist
body: <p>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.</p>
  <p>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?</p>
  <p>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.</p>
  <p>Events Class</p>
  <pre><code>@Entity
  @Table(name = &quot;events&quot;)
  public class Events {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = &quot;event_name&quot;)
      private String eventName;
  
      @Column(name= &quot;event_date&quot;)
      private Date eventDate;
  
      @ManyToMany(cascade = {
                      CascadeType.PERSIST,
                      CascadeType.MERGE
              })
      @JoinTable(name = &quot;events_channels&quot;,
              joinColumns = { @JoinColumn(name = &quot;events_id&quot;) },
              inverseJoinColumns = { @JoinColumn(name = &quot;channels_id&quot;) })
      private List&lt;Channels&gt; channels;
  
      @ManyToMany(cascade = {
              CascadeType.PERSIST,
              CascadeType.MERGE
      })
      private List&lt;Teams&gt; homeTeam;
  
      @ManyToMany(cascade = {
              CascadeType.PERSIST,
              CascadeType.MERGE
      })
      private List&lt;Teams&gt; awayTeam;
  
      public Events() {}
  }
  </code></pre>
  <p>Channels Class</p>
  <pre><code>@Entity
  @Table(name=&quot;channels&quot;)
  public class Channels {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = &quot;channel_name&quot;)
      private String channelName;
  
      @ManyToMany(fetch = FetchType.LAZY,
              cascade = {
                      CascadeType.REMOVE
              },
              mappedBy = &quot;channels&quot;)
      @JsonIgnore
      private List&lt;Events&gt; events;
  
      public Channels() {}
  }
  </code></pre>
  <p>I also have a data.sql where I put the following dummy data into the ManyToMany join table.</p>
  <pre><code>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);
  </code></pre>