There is a good article with some deletion tricks explained, but there is something I still can't grasp.
Let's take entity from that article.
public class Post {
@OneToMany(
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Comment> comments = new ArrayList<>();
}
And let's say I want to remove ALL comments at once. There are multiple approaches:
post.comments.clear()this would issue N delete statements like:delete from comment where comment_id=n- iterator based approach with
comment.post = null, iterator.remove()Same story withdelete from comment where comment_id=n entityManager.remove(post)Same story.
I can't understand why in all those cases I don't see a query like
delete from comment where parent=X
Is there any specific reason for that?
Am I right that the best solution to achieve it is to execute jpql/hql query directly: delete from comment where parent=X. Are there any downsides of that approach? (i.e. hibernate caches are not updated or something)