Skip to content

Commit e9af66e

Browse files
committed
Merge entity collections
1 parent 6795df0 commit e9af66e

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public ApplicationRunner init() {
2828
System.out.println("------------------- Joana Nimar's Books --------------------");
2929
List<Book> books = bookstoreService.fetchBooksOfAuthor("Joana Nimar");
3030

31-
books.forEach(b -> System.out.println("Title: "
32-
+ b.getTitle() + " | id:(" + b.getId() + ")"));
31+
books.forEach(b -> System.out.println(b));
3332

3433
System.out.println("---------- Books of Joana Nimar Updated Detached ------------");
3534

@@ -45,14 +44,12 @@ public ApplicationRunner init() {
4544
book.setIsbn("005-JN");
4645
books.add(book);
4746

48-
books.forEach(b -> System.out.println("Title: "
49-
+ b.getTitle() + " | id:(" + b.getId() + ")"));
50-
47+
books.forEach(b -> System.out.println(b));
48+
5149
System.out.println("----------------- Books of Joana Nimar Merged ----------------");
5250
bookstoreService.updateBooksOfAuthor("Joana Nimar", books);
5351

54-
books.forEach(b -> System.out.println("Title: "
55-
+ b.getTitle() + " | id:(" + b.getId() + ")"));
52+
books.forEach(b -> System.out.println(b));
5653
};
5754
}
58-
}
55+
}

HibernateSpringBootMergeCollections/src/main/java/com/bookstore/entity/Book.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public void setAuthor(Author author) {
6060
@Override
6161
public boolean equals(Object obj) {
6262

63+
if(obj == null) {
64+
return false;
65+
}
66+
6367
if (this == obj) {
6468
return true;
6569
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ public void updateBooksOfAuthor(String name, List<Book> books) {
3535

3636
// Remove the existing database rows that are no
3737
// longer found in the incoming collection (books)
38-
author.getBooks().removeIf(b -> !books.contains(b));
38+
List<Book> toRemove = author.getBooks().stream()
39+
.filter(b -> !books.contains(b))
40+
.collect(Collectors.toList());
41+
toRemove.forEach(b -> author.removeBook(b));
3942

4043
// Update the existing database rows which can be found
4144
// in the incoming collection (books)
4245
List<Book> newBooks = books.stream()
4346
.filter(b -> !author.getBooks().contains(b))
44-
.collect(Collectors.toList());
47+
.collect(Collectors.toList());
4548

4649
books.stream()
4750
.filter(b -> !newBooks.contains(b))
4851
.forEach((b) -> {
49-
b.setAuthor(author);
52+
b.setAuthor(author);
5053
Book mergedBook = bookRepository.save(b);
5154
author.getBooks().set(
5255
author.getBooks().indexOf(mergedBook),

HibernateSpringBootMergeCollections/src/main/resources/data-mysql.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ insert into author (age, name, genre, id) values (34, "Joana Nimar", "History",
33
insert into book (isbn, title, author_id, id) values ("001-JN", "A History of Ancient Prague", 2, 1);
44
insert into book (isbn, title, author_id, id) values ("002-JN", "A People's History", 2, 2);
55
insert into book (isbn, title, author_id, id) values ("001-MJ", "The Beatles Anthology", 1, 3);
6-
insert into book (isbn, title, author_id, id) values ("001-OG", "Carrie", 2, 4);
6+
insert into book (isbn, title, author_id, id) values ("007-JN", "Carrie", 2, 4);

0 commit comments

Comments
 (0)