Skip to content

Commit 3c6fc73

Browse files
committed
Batch deletes via SQL cascade delete
1 parent 337ca09 commit 3c6fc73

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

HibernateSpringBootBatchDeleteCascadeDelete/src/main/java/com/bookstore/service/BookstoreService.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void deleteAuthorsAndBooksViaDeleteAllInBatch() {
5151
// deleting the authors will delete the books as well
5252
@Transactional
5353
public void deleteAuthorsAndBooksViaDeleteInBatch() {
54-
List<Author> authors = authorRepository.findAll();
54+
List<Author> authors = authorRepository.fetchAuthorsAndBooks(60);
5555

5656
authorRepository.deleteInBatch(authors);
5757
}
@@ -60,15 +60,17 @@ public void deleteAuthorsAndBooksViaDeleteInBatch() {
6060
// the authors will be deleted in batches; the books will be deleted as well
6161
@Transactional
6262
public void deleteAuthorsAndBooksViaDeleteAll() {
63-
authorRepository.deleteAll(); // for a collection of certain Authors use deleteAll(Iterable<? extends T> entities)
63+
List<Author> authors = authorRepository.fetchAuthorsAndBooks(60);
64+
65+
authorRepository.deleteAll(authors); // for deleting all Author use deleteAll()
6466
}
6567

6668
// good if you need to delete in a classical batch approach
6769
// the authors will be deleted in batches; the books will be deleted as well
6870
@Transactional
6971
public void deleteAuthorsAndBooksViaDelete() {
70-
71-
List<Author> authors = authorRepository.findAll();
72+
List<Author> authors = authorRepository.fetchAuthorsAndBooks(60);
73+
7274
authors.forEach(authorRepository::delete);
7375
}
7476
}

0 commit comments

Comments
 (0)