I have the below JPA entities defined with bidirectional one-to-many mapping.
I observed that delete SQL statement is fired when a comment is removed from a list on post(post.removeComment(...)
) even without setting orphan removal = true or making the post reference "null" on the comment .
What is the significance of Orphan removal = true when trying to delete items from a collection in JPA as I see the deletion is working fine even without it.
@Entity(name = "Post")
@Table(name = "post")
public class Post {
@Id
@GeneratedValue
private Long id;
private String title;
@OneToMany(
mappedBy = "post",
cascade = CascadeType.ALL
)
private List<PostComment> comments = new ArrayList<>();
public Post addComment(PostComment comment) {
comments.add(comment);
comment.setPost(this);
return this;
}
public Post removeComment(PostComment comment) {
comments.remove(comment);
return this;
}
}
@Entity(name = "PostComment")
@Table(name = "post_comment")
public class PostComment {
@Id
@GeneratedValue
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Post post;
private String review;
}