Skip to content

Commit 3e37dfa

Browse files
Batch deletes via SQL cascade delete
1 parent f0a4900 commit 3e37dfa

File tree

1 file changed

+2
-2
lines changed
  • HibernateSpringBootBatchDeleteCascadeDelete

1 file changed

+2
-2
lines changed

HibernateSpringBootBatchDeleteCascadeDelete/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
**Description:** Batch deletes in MySQL via `ON DELETE CASCADE`. Auto-generated database schema will contain the `ON DELETE CASCADE` directive.
66

7-
**Note:** Spring `deleteAllInBatch()` and `deleteInBatch()` don't use delete batching and don't take advantage of optimistic locking mechanism to prevent *lost updates*. They trigger *bulk* operations via `Query.executeUpdate()`, therefore, the Persistent Context is not synchronized accordingly (it is advisable to flush (before delete) and close/clear (after delete) the Persistent Context accordingly to avoid issues created by unflushed (if any) or outdated (if any) entities). The first one simply triggers a `delete from entity_name` statement, while the second one triggers a `delete from entity_name where id=? or id=? or id=? ...` statement. Both of them take advantage on `ON DELETE CASCADE` and are very efficient. For delete in batches rely on `deleteAll()`, `deleteAll(Iterable<? extends T> entities)` or `delete()` method. Behind the scene, the two flavors of `deleteAll()` relies on `delete()`. Mixing batching with database automatic actions (`ON DELETE CASCADE`) will result in a partially synchronized Persistent Context.
7+
**Note:** Spring `deleteAllInBatch()` and `deleteInBatch()` don't use delete batching and don't take advantage of cascading removal, `orphanRemoval` and automatic optimistic locking mechanism to prevent *lost updates* (e.g., `@Version` is ignored), but both of them take advantage on `ON DELETE CASCADE` and are very efficient. They trigger *bulk* operations via `Query.executeUpdate()`, therefore, the Persistence Context is not synchronized accordingly (it's up to you to flush (before delete) and close/clear (after delete) the Persistence Context accordingly to avoid issues created by unflushed (if any) or outdated (if any) entities). The first one simply triggers a `delete from entity_name` statement, while the second one triggers a `delete from entity_name where id=? or id=? or id=? ...` statement. For delete in batches rely on `deleteAll()`, `deleteAll(Iterable<? extends T> entities)` or `delete()` method. Behind the scene, the two flavors of `deleteAll()` relies on `delete()`. Mixing batching with database automatic actions (`ON DELETE CASCADE`) will result in a partially synchronized Persistent Context.
88

99
**Key points:**
1010
- in this application, we have a `Author` entity and each author can have several `Book` (*one-to-many*)
1111
- first, we remove `orphanRemoval` or set it to `false`
1212
- second, we use only `CascadeType.PERSIST` and `CascadeType.MERGE`
1313
- third, we set `@OnDelete(action = OnDeleteAction.CASCADE)` next to `@OneToMany`
1414
- fourth, we set `spring.jpa.properties.hibernate.dialect` to `org.hibernate.dialect.MySQL5InnoDBDialect` (or, `MySQL8Dialect`)
15-
- fifth, we run through each `deleteFoo()` method
15+
- fifth, we run through a set of `deleteFoo()` methods that uses *bulk* and batching deletes as well
1616

1717
**Output example:**
1818

0 commit comments

Comments
 (0)