Spring Boot v2.5.2
I have comment entity that is associated with user and course ( both bidirectional @ManyToOne )
public class Comment {
@Id
@GeneratedValue(generator = "inc")
@GenericGenerator(name = "inc", strategy = "increment")
private int commentId;
@NotBlank(message = "Your comment is empty!")
private String text;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "course_id")
private Course course;
}
public class Course {
@Id
@GeneratedValue(generator = "inc")
@GenericGenerator(name = "inc", strategy = "increment")
private int courseId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "course", fetch = FetchType.EAGER)
private Set<Comment> comments;
}
public class User {
@Id
@GeneratedValue(generator = "inc")
@GenericGenerator(name = "inc", strategy = "increment")
private int userId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private Set<Comment> comments;
In both associations comment is the owner side so I can remove the comment and then all associations (for this comment) should be removed as well, right? ( it works that way when I have SINGLE association - I mean a one-to-many association between two entities, that's it )
Anyway I can't delete the comment using deleteById(commentId) method from JpaRepository. I enabled the logs for Hibernate and after calling this method I get:
2021-08-03 16:43:35.904 DEBUG 13524 --- [nio-8080-exec-3] org.hibernate.SQL : select comment0_.comment_id as comment_1_1_0_, comment0_.course_id as course_i3_1_0_, comment0_.text as text2_1_0_, comment0_.user_id as user_id4_1_0_, course1_.course_id as course_i1_4_1_, course1_.description as descript2_4_1_, course1_.price as price3_4_1_, course1_.promotion as promotio4_4_1_, course1_.title as title5_4_1_, categories2_.course_id as course_i2_2_2_, category3_.category_id as category1_2_2_, category3_.category_id as category1_0_3_, category3_.name as name2_0_3_, user4_.user_id as user_id1_5_4_, user4_.name as name2_5_4_ from comments comment0_ left outer join courses course1_ on comment0_.course_id=course1_.course_id left outer join course_category categories2_ on course1_.course_id=categories2_.course_id left outer join categories category3_ on categories2_.category_id=category3_.category_id left outer join users user4_ on comment0_.user_id=user4_.user_id where comment0_.comment_id=?
2021-08-03 16:43:35.939 DEBUG 13524 --- [nio-8080-exec-3] org.hibernate.SQL : select comments0_.course_id as course_i3_1_0_, comments0_.comment_id as comment_1_1_0_, comments0_.comment_id as comment_1_1_1_, comments0_.course_id as course_i3_1_1_, comments0_.text as text2_1_1_, comments0_.user_id as user_id4_1_1_, user1_.user_id as user_id1_5_2_, user1_.name as name2_5_2_ from comments comments0_ left outer join users user1_ on comments0_.user_id=user1_.user_id where comments0_.course_id=?
It even does not throw any error!
Can someone explain why it doesn't work ?
EDIT
I Found a solution that makes me much more confused. I decided to break the associations manually (on trial). Look at my code that works:
public void deleteComment(Integer commentId) {
Comment target = repository.findById(commentId)
.orElseThrow(() -> new IllegalArgumentException("No Comment with given id"));
Course targetCourse = target.getCourse();
targetCourse.setComments(null);
User targetUser = target.getUser();
targetUser.setComments(null);
repository.deleteById(commentId);
}
(I realize that I shouldn't set comments to null only on a comment set without the one I want to delete but I had a problem with that and this is basically just an attempt)
And my logs from Hibernate:
2021-08-03 18:05:14.610 DEBUG 1228 --- [nio-8080-exec-1] org.hibernate.SQL : select comment0_.comment_id as comment_1_1_0_, comment0_.course_id as course_i3_1_0_, comment0_.text as text2_1_0_, comment0_.user_id as user_id4_1_0_, course1_.course_id as course_i1_4_1_, course1_.description as descript2_4_1_, course1_.price as price3_4_1_, course1_.promotion as promotio4_4_1_, course1_.title as title5_4_1_, categories2_.course_id as course_i2_2_2_, category3_.category_id as category1_2_2_, category3_.category_id as category1_0_3_, category3_.name as name2_0_3_, user4_.user_id as user_id1_5_4_, user4_.name as name2_5_4_ from comments comment0_ left outer join courses course1_ on comment0_.course_id=course1_.course_id left outer join course_category categories2_ on course1_.course_id=categories2_.course_id left outer join categories category3_ on categories2_.category_id=category3_.category_id left outer join users user4_ on comment0_.user_id=user4_.user_id where comment0_.comment_id=?
2021-08-03 18:05:14.655 DEBUG 1228 --- [nio-8080-exec-1] org.hibernate.SQL : select comments0_.course_id as course_i3_1_0_, comments0_.comment_id as comment_1_1_0_, comments0_.comment_id as comment_1_1_1_, comments0_.course_id as course_i3_1_1_, comments0_.text as text2_1_1_, comments0_.user_id as user_id4_1_1_, user1_.user_id as user_id1_5_2_, user1_.name as name2_5_2_ from comments comments0_ left outer join users user1_ on comments0_.user_id=user1_.user_id where comments0_.course_id=?
2021-08-03 18:05:14.740 DEBUG 1228 --- [nio-8080-exec-1] org.hibernate.SQL : delete from comments where comment_id=?
Why does it make me more confused? As this should be done automatically (it works like this in my other application where there is a single association - I mentioned about it above). What's more I updated my associations not by owner side. comment is owner side so comment should be resposnsible for any changes in the assocation, right? ... Somewhere in my reasoning I must be wrong !
deleteByIdis called (I know because I can see the hibernate logs) but there is nodeletestatement, onlyselect- Monoxyd