Hi,
I am using Spring Security to secure my Spring Boot app and have followed it's convention to create and store users -- i.e. each users can have roles.
There's many to many relationship between users and roles and the junction table is created automatically by Spring.
Now when I am trying to delete a particular user and am getting this error :
java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`sudoku2`.`user_roles`, CONSTRAINT `FKrhfovtciq1l558cw6udg0h0d3` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`))
To fix this I have used the Cascade annotation in my User entity but this error isn't going away.
//User entity
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User {
// other code
@ManyToMany(fetch = FetchType.EAGER)
@Cascade(value = CascadeType.DELETE)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
private List<Role> roles = new ArrayList<>();
//getters and setters and other code ...
}
Can anyone help me with it ?
Thanks