Skip to content

Commit c31f782

Browse files
committed
Fetch association in batches
1 parent 082f78d commit c31f782

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

HibernateSpringBootLoadBatchAssociation/src/main/java/com/bookstore/MainApplication.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public static void main(String[] args) {
2020
@Bean
2121
public ApplicationRunner init() {
2222
return args -> {
23-
bookstoreService.displayAuthorsAndBooks();
23+
// System.out.println("\n\nLoad authors with books:");
24+
// bookstoreService.displayAuthorsAndBooks();
25+
26+
System.out.println("\n\nLoad books with authors:");
27+
bookstoreService.displayBooksAndAuthors();
2428
};
2529
}
2630
}

HibernateSpringBootLoadBatchAssociation/src/main/java/com/bookstore/entity/Author.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.annotations.BatchSize;
1414

1515
@Entity
16+
@BatchSize(size = 3)
1617
public class Author implements Serializable {
1718

1819
private static final long serialVersionUID = 1L;
@@ -27,7 +28,7 @@ public class Author implements Serializable {
2728

2829
@OneToMany(cascade = CascadeType.ALL,
2930
mappedBy = "author", orphanRemoval = true)
30-
@BatchSize(size = 3)
31+
//@BatchSize(size = 3)
3132
private List<Book> books = new ArrayList<>();
3233

3334
public void addBook(Book book) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Book;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface BookRepository extends JpaRepository<Book, Long> {
9+
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.bookstore.repository.AuthorRepository;
44
import com.bookstore.entity.Author;
5+
import com.bookstore.entity.Book;
6+
import com.bookstore.repository.BookRepository;
57
import java.util.List;
68
import org.springframework.stereotype.Service;
79
import org.springframework.transaction.annotation.Transactional;
@@ -10,10 +12,13 @@
1012
public class BookstoreService {
1113

1214
private final AuthorRepository authorRepository;
15+
private final BookRepository bookRepository;
1316

14-
public BookstoreService(AuthorRepository authorRepository) {
17+
public BookstoreService(AuthorRepository authorRepository,
18+
BookRepository bookRepository) {
1519

1620
this.authorRepository = authorRepository;
21+
this.bookRepository = bookRepository;
1722
}
1823

1924
@Transactional(readOnly = true)
@@ -27,4 +32,15 @@ public void displayAuthorsAndBooks() {
2732
+ author.getBooks().size() + ", " + author.getBooks());
2833
}
2934
}
35+
36+
@Transactional(readOnly = true)
37+
public void displayBooksAndAuthors() {
38+
39+
List<Book> books = bookRepository.findAll();
40+
41+
for (Book book : books) {
42+
System.out.println("Book: " + book.getTitle());
43+
System.out.println("Author: " + book.getAuthor());
44+
}
45+
}
3046
}

0 commit comments

Comments
 (0)