Entity state transitions
JPA translates entity state transitions to SQL statements, like INSERT, UPDATE or DELETE.

When you persist
an entity, you are scheduling the INSERT statement to be executed when the EntityManager
is flushed, either automatically or manually.
when you remove
an entity, you are scheduling the DELETE statement, which will be executed when the Persistence Context is flushed.
Cascading entity state transitions
For convenience, JPA allows you to propagate entity state transitions from parent entities to child one.
So, if you have a parent Post
entity that has a @OneToMany
association with the PostComment
child entity:

The comments
collection in the Post
entity is mapped as follows:
@OneToMany(
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Comment> comments = new ArrayList<>();
CascadeType.ALL
The cascade
attribute tells the JPA provider to pass the entity state transition from the parent Post
entity to all PostComment
entities contained in the comments
collection.
So, if you remove the Post
entity:
Post post = entityManager.find(Post.class, 1L);
assertEquals(2, post.getComments().size());
entityManager.remove(post);
The JPA provider is going to remove the PostComment
entity first, and when all child entities are deleted, it will delete the Post
entity as well:
DELETE FROM post_comment WHERE id = 1
DELETE FROM post_comment WHERE id = 2
DELETE FROM post WHERE id = 1
Orphan removal
When you set the orphanRemoval
attribute to true
, the JPA provider is going to schedule a remove
operation when the child entity is removed from the collection.
So, in our case,
Post post = entityManager.find(Post.class, 1L);
assertEquals(2, post.getComments().size());
PostComment postComment = post.getComments().get(0);
assertEquals(1L, postComment.getId());
post.getComments().remove(postComment);
The JPA provider is going to remove the associated post_comment
record since the PostComment
entity is no longer referenced in the comments
collection:
DELETE FROM post_comment WHERE id = 1
ON DELETE CASCADE
The ON DELETE CASCADE
is defined at the FK level:
ALTER TABLE post_comment
ADD CONSTRAINT fk_post_comment_post_id
FOREIGN KEY (post_id) REFERENCES post
ON DELETE CASCADE;
Once you do that, if you delete a post
row:
DELETE FROM post WHERE id = 1
All the associated post_comment
entities are removed automatically by the database engine. However, this can be a very dangerous operation if you delete a root entity by mistake.
Conclusion
The advantage of the JPA cascade
and orphanRemoval
options is that you can also benefit from optimistic locking to prevent lost updates.
If you use the JPA cascading mechanism, you don't need to use DDL-level ON DELETE CASCADE
, which can be a very dangerous operation if you remove a root entity that has many child entities on multiple levels.