|
2 | 2 |
|
3 | 3 | **Description:** Batch deletes in MySQL via `orphanRemoval=true`. |
4 | 4 |
|
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. |
6 | 6 |
|
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 | | - |
| 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) |
17 | 13 |
|
18 | 14 | ------------------------------- |
19 | 15 |
|
|
0 commit comments