Skip to content

Commit 337ca09

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

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.bookstore.repository;
22

33
import com.bookstore.entity.Author;
4+
import java.util.List;
5+
import javax.persistence.QueryHint;
6+
import static org.hibernate.jpa.QueryHints.HINT_PASS_DISTINCT_THROUGH;
47
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
9+
import org.springframework.data.jpa.repository.QueryHints;
510
import org.springframework.stereotype.Repository;
611

712
@Repository
813
public interface AuthorRepository extends JpaRepository<Author, Long> {
14+
15+
@QueryHints(value = @QueryHint(name = HINT_PASS_DISTINCT_THROUGH, value = "false"))
16+
@Query(value = "SELECT DISTINCT a FROM Author a JOIN FETCH a.books b WHERE a.age < ?1")
17+
List<Author> fetchAuthorsAndBooks(int age);
918
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void batchAuthorsAndBooks() {
2626
Author author = new Author();
2727
author.setName("Name_" + i);
2828
author.setGenre("Genre_" + i);
29-
author.setAge(18 + i);
29+
author.setAge((int) ((Math.random() + 0.1) * 100));
3030

3131
for (int j = 0; j < 5; j++) {
3232
Book book = new Book();

0 commit comments

Comments
 (0)