我需要删除 User'a,但由于他有连接(订阅和订阅者),对象没有被删除,我该如何正确实现呢?
抛出的异常:
Cannot delete or update a parent row: a foreign key constraint fails (`sweater_db`.`sweater_user_subscribers`, CONSTRAINT `user_role_subscribers_subscriber_fk` FOREIGN KEY (`subscriber_id`) REFERENCES `sweater_user` (`id`))
用户
@Entity
@Table(name = "sweater_user")
public class User implements UserDetails{
private final static long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Username should not be empty")
private String username;
@NotBlank(message = "Password should not be empty")
private String password;
@Email(message = "Email is not correct")
@NotBlank(message = "Email should not be empty")
private String email;
private String activationCode;
private boolean active;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(
name = "sweater_user_role",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
private Collection<Role> roles;
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Collection<Message> messages;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "sweater_user_subscribers",
joinColumns = @JoinColumn(name = "channel_id"),
inverseJoinColumns = @JoinColumn(name = "subscriber_id")
)
private Set<User> subscribers = new HashSet<>();
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "sweater_user_subscriptions",
joinColumns = @JoinColumn(name = "subscriber_id"),
inverseJoinColumns = @JoinColumn(name = "channel_id")
)
private Set<User> subscriptions = new HashSet<>();