I have two entities, user and movies. They're manytomany bidirectional relationship. My problem is that when I delete a movie via my controller it also removes all related with that movie entities. My user and this user roles and the movie entity. What have I to do, to get rid just off movie entity from the table when delete it and keep the user with his roles instead of removing them all.
@Data
@ToString
@EqualsAndHashCode
@Entity
@Table(name = "movies")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Column(name = "release_date")
@Temporal(TemporalType.DATE)
private Date release_date;
@Column(name = "country")
private String country;
@Column(name = "category")
private String category;
@ManyToMany(mappedBy = "movies")
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Set<User> users = new HashSet<>();
@Data
@ToString
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Transient
private String confirmPassword;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "users_roles", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Set<Role> roles = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "users_movies", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "movie_id", referencedColumnName = "id")})
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Set<Movie> movies = new HashSet<>();