File tree Expand file tree Collapse file tree 4 files changed +33
-3
lines changed
HibernateSpringBootLoadBatchAssociation/src/main/java/com/bookstore Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Original file line number Diff line number Diff 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 \n Load books with authors:" );
27+ bookstoreService .displayBooksAndAuthors ();
2428 };
2529 }
2630}
Original file line number Diff line number Diff line change 1313import org .hibernate .annotations .BatchSize ;
1414
1515@ Entity
16+ @ BatchSize (size = 3 )
1617public 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 ) {
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 22
33import com .bookstore .repository .AuthorRepository ;
44import com .bookstore .entity .Author ;
5+ import com .bookstore .entity .Book ;
6+ import com .bookstore .repository .BookRepository ;
57import java .util .List ;
68import org .springframework .stereotype .Service ;
79import org .springframework .transaction .annotation .Transactional ;
1012public 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}
You can’t perform that action at this time.
0 commit comments