Skip to content

Commit e52af67

Browse files
committed
Merge entity collections
1 parent 1c244a6 commit e52af67

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,33 @@ public static void main(String[] args) {
2525
public ApplicationRunner init() {
2626
return args -> {
2727

28-
System.out.println("------------------- Joana Nimar's Books --------------------");
29-
List<Book> books = bookstoreService.fetchBooksOfAuthor("Joana Nimar");
28+
System.out.println("\n------------------- Joana Nimar's Books --------------------");
29+
List<Book> detachedBooks = bookstoreService.fetchBooksOfAuthor("Joana Nimar");
3030

31-
books.forEach(b -> System.out.println(b));
31+
detachedBooks.forEach(b -> System.out.println(b));
3232

33-
System.out.println("---------- Books of Joana Nimar Updated Detached ------------");
33+
System.out.println("\n---------- Books of Joana Nimar updated in detached state------------");
3434

3535
// ,update first book title
36-
books.get(0).setTitle("A History of Ancient Rome");
36+
detachedBooks.get(0).setTitle("A History of Ancient Rome");
3737

3838
// remove second title
39-
books.remove(1);
39+
detachedBooks.remove(1);
4040

4141
// add a new book
4242
Book book = new Book();
4343
book.setTitle("History In 100 Minutes");
4444
book.setIsbn("005-JN");
45-
books.add(book);
45+
detachedBooks.add(book);
4646

47-
books.forEach(b -> System.out.println(b));
47+
detachedBooks.forEach(b -> System.out.println(b));
4848

49-
System.out.println("----------------- Books of Joana Nimar Merged ----------------");
50-
bookstoreService.updateBooksOfAuthor("Joana Nimar", books);
49+
System.out.println("\n----------------- Merging books of Joana Nimar ----------------");
50+
bookstoreService.updateBooksOfAuthor("Joana Nimar", detachedBooks);
5151

52+
System.out.println("\n----------------- Books of Joana Nimar After Merge ----------------");
53+
List<Book> books = bookstoreService.fetchBooksOfAuthor("Joana Nimar");
54+
5255
books.forEach(b -> System.out.println(b));
5356
};
5457
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public void updateBooksOfAuthor(String name, List<Book> detachedBooks) {
3434
System.out.println("-------------------------------------------------");
3535

3636
// Remove the existing database rows that are no
37-
// longer found in the incoming collection (books)
38-
List<Book> toRemove = author.getBooks().stream()
37+
// longer found in the incoming collection (detachedBooks)
38+
List<Book> booksToRemove = author.getBooks().stream()
3939
.filter(b -> !detachedBooks.contains(b))
4040
.collect(Collectors.toList());
41-
toRemove.forEach(b -> author.removeBook(b));
41+
booksToRemove .forEach(b -> author.removeBook(b));
4242

4343
// Update the existing database rows which can be found
44-
// in the incoming collection (books)
44+
// in the incoming collection (detachedBooks)
4545
List<Book> newBooks = detachedBooks.stream()
4646
.filter(b -> !author.getBooks().contains(b))
4747
.collect(Collectors.toList());
@@ -52,8 +52,7 @@ public void updateBooksOfAuthor(String name, List<Book> detachedBooks) {
5252
b.setAuthor(author);
5353
Book mergedBook = bookRepository.save(b);
5454
author.getBooks().set(
55-
author.getBooks().indexOf(mergedBook),
56-
mergedBook);
55+
author.getBooks().indexOf(mergedBook), mergedBook);
5756
});
5857

5958
// Add the rows found in the incoming collection,

0 commit comments

Comments
 (0)