JPA/Hibernate why removal of all children doesn't issue delete by parent id

Viewed 21

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:

  1. post.comments.clear() this would issue N delete statements like: delete from comment where comment_id=n
  2. iterator based approach with comment.post = null, iterator.remove() Same story with delete from comment where comment_id=n
  3. 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)

0 Answers
Related