Skip to content

Commit 986b3af

Browse files
Batch deletes (including associations)
1 parent a577f2e commit 986b3af

File tree

1 file changed

+7
-11
lines changed
  • HibernateSpringBootBatchDeleteOrphanRemoval

1 file changed

+7
-11
lines changed

HibernateSpringBootBatchDeleteOrphanRemoval/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
**Description:** Batch deletes in MySQL via `orphanRemoval=true`.
44

5-
**Note:** Spring `deleteAllInBatch()` and `deleteInBatch()` don't use batching. 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. Rely on `delete()` method.
5+
**Note:** Spring `deleteAllInBatch()` and `deleteInBatch()` don't use batching and don't take advantage of `orphanRemoval=true`. 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. Using these methods for deleting a parent entities and the associated entites (children) requires explicit calls for both sides. For batching rely on `deleteAll()` or even better, on `delete()` method.
66

7-
**Key points:**\
8-
- in this example, we have a `Tournament` entity and each tournament can have several `TennisPlayer` (*one-to-many*)\
9-
- first, we use `orphanRemoval=true` and only `CascadeType.PERSIST` and `CascadeType.MERGE`\
10-
- second, we dissociate all `TennisPlayer` from the corresponding `Tournament`\
11-
- third, we explicitly (manually) flush the persistent context (this will delete in batch all `TennisPlayer` thanks to `orphanRemoval=true`; if this is set to `false`, you will obtain a bunch of updates instead of deletes)\
12-
- forth, we delete all `Tournament` via the `delete()` method (since we have dissaciated all `TennisPlayer`, the `Tournament` deletion will take advantage of batching as well)
13-
14-
**Output example:**
15-
16-
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/HibernateSpringBootBatchDeleteOrphanRemoval/batch_delete.png)
7+
**Key points for using `delete()`:**\
8+
- in this example, we have a `Author` entity and each author can have several `Book` (*one-to-many*)\
9+
- first, we use `orphanRemoval=true` and `CascadeType.ALL`\
10+
- second, we dissociate all `Book` from the corresponding `Author`\
11+
- third, we explicitly (manually) flush the persistent context (this will delete in batch all `Book` thanks to `orphanRemoval=true`; if this is set to `false`, you will obtain a bunch of updates instead of deletes)\
12+
- forth, we delete all `Author` via the `deleteAll()` or `delete()` method (since we have dissaciated all `Book`, the `Author` deletion will take advantage of batching as well)
1713

1814
-------------------------------
1915

0 commit comments

Comments
 (0)