#How do I delete a User from my db ? Project created using Spring Security & Spring Boot ?

1 messages · Page 1 of 1 (latest)

sleek coral
#

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

analog fogBOT
#

<@&1004656351647117403> please have a look, thanks.

analog fogBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.