Skip to content

Commit dd62356

Browse files
committed
Best one-to-many mapping
1 parent 9dbb77a commit dd62356

File tree

5 files changed

+39
-42
lines changed

5 files changed

+39
-42
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public static void main(String[] args) {
2222
@Bean
2323
public ApplicationRunner init() {
2424
return args -> {
25-
System.out.println("\nAdd author with books ...");
26-
bookstoreService.addAuthorWithBooks();
25+
System.out.println("\nInsert author with books ...");
26+
bookstoreService.insertAuthorWithBooks();
2727

28-
System.out.println("\nRemove a book of an author...");
29-
bookstoreService.removeBookOfAuthor();
28+
System.out.println("\nDelete a book of an author...");
29+
bookstoreService.deleteBookOfAuthor();
3030

31-
System.out.println("\nRemove all book of an author...");
32-
bookstoreService.removeAllBooksOfAuthor();
31+
System.out.println("\nDelete all book of an author...");
32+
bookstoreService.deleteAllBooksOfAuthor();
3333
};
3434
}
3535
}

HibernateSpringBootOneToManyBidirectional/src/main/java/com/bookstore/repository/AuthorRepository.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import com.bookstore.entity.Author;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
56
import org.springframework.stereotype.Repository;
67
import org.springframework.transaction.annotation.Transactional;
78

89
@Repository
10+
@Transactional(readOnly=true)
911
public interface AuthorRepository extends JpaRepository<Author, Long> {
10-
11-
@Transactional(readOnly=true)
12-
Author findByName(String name);
12+
13+
@Query("SELECT a FROM Author a JOIN FETCH a.books WHERE a.name = ?1")
14+
Author fetchByName(String name);
1315
}

HibernateSpringBootOneToManyBidirectional/src/main/java/com/bookstore/repository/BookRepository.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import com.bookstore.entity.Book;
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
6-
import org.springframework.transaction.annotation.Transactional;
76

87
@Repository
98
public interface BookRepository extends JpaRepository<Book, Long> {
10-
11-
@Transactional(readOnly = true)
12-
Book findByTitle(String title);
139
}

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.bookstore.service;
22

3-
import com.bookstore.repository.BookRepository;
43
import com.bookstore.repository.AuthorRepository;
54
import com.bookstore.entity.Author;
65
import com.bookstore.entity.Book;
@@ -9,45 +8,42 @@
98

109
@Service
1110
public class BookstoreService {
12-
11+
1312
private final AuthorRepository authorRepository;
14-
private final BookRepository bookRepository;
1513

16-
public BookstoreService(AuthorRepository authorRepository,
17-
BookRepository bookRepository) {
14+
public BookstoreService(AuthorRepository authorRepository) {
1815

1916
this.authorRepository = authorRepository;
20-
this.bookRepository = bookRepository;
2117
}
22-
23-
public void addAuthorWithBooks(){
24-
18+
19+
public void insertAuthorWithBooks() {
20+
2521
Author author = new Author();
2622
author.setName("Alicia Tom");
2723
author.setAge(38);
2824
author.setGenre("Anthology");
29-
25+
3026
Book book = new Book();
3127
book.setIsbn("001-AT");
3228
book.setTitle("The book of swords");
33-
29+
3430
author.addBook(book); // use addBook() helper
35-
31+
3632
authorRepository.save(author);
3733
}
38-
34+
3935
@Transactional
40-
public void removeBookOfAuthor() {
41-
42-
Author author = authorRepository.findByName("Alicia Tom");
43-
Book book = bookRepository.findByTitle("The book of swords");
44-
36+
public void deleteBookOfAuthor() {
37+
38+
Author author = authorRepository.fetchByName("Alicia Tom");
39+
Book book = author.getBooks().get(0);
40+
4541
author.removeBook(book); // use removeBook() helper
4642
}
47-
43+
4844
@Transactional
49-
public void removeAllBooksOfAuthor() {
50-
Author author = authorRepository.findByName("Joana Nimar");
45+
public void deleteAllBooksOfAuthor() {
46+
Author author = authorRepository.fetchByName("Joana Nimar");
5147
author.removeBooks(); // use removeBooks() helper
5248
}
5349

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
insert into author (age, name, genre, id) values (23, "Mark Janel", "Anthology", 1);
2-
insert into author (age, name, genre, id) values (43, "Olivia Goy", "Horror", 2);
3-
insert into author (age, name, genre, id) values (51, "Quartis Young", "Anthology", 3);
4-
insert into author (age, name, genre, id) values (34, "Joana Nimar", "History", 4);
5-
insert into book (isbn, title, author_id, id) values ("001-JN", "A History of Ancient Prague", 4, 1);
6-
insert into book (isbn, title, author_id, id) values ("002-JN", "A People's History", 4, 2);
7-
insert into book (isbn, title, author_id, id) values ("001-MJ", "The Beatles Anthology", 1, 3);
8-
insert into book (isbn, title, author_id, id) values ("001-OG", "Carrie", 2, 4);
1+
-- insert authors
2+
INSERT INTO `author` (`age`, `name`, `genre`, `id`) VALUES (23, "Mark Janel", "Anthology", 1);
3+
INSERT INTO `author` (`age`, `name`, `genre`, `id`) VALUES (43, "Olivia Goy", "Horror", 2);
4+
INSERT INTO `author` (`age`, `name`, `genre`, `id`) VALUES (51, "Quartis Young", "Anthology", 3);
5+
INSERT INTO `author` (`age`, `name`, `genre`, `id`) VALUES (34, "Joana Nimar", "History", 4);
6+
7+
-- insert books
8+
INSERT INTO `book` (`isbn`, `title`, `author_id`, `id`) VALUES ("001-JN", "A History of Ancient Prague", 4, 1);
9+
INSERT INTO `book` (`isbn`, `title`, `author_id`, `id`) VALUES ("002-JN", "A People's History", 4, 2);
10+
INSERT INTO `book` (`isbn`, `title`, `author_id`, `id`) VALUES ("001-MJ", "The Beatles Anthology", 1, 3);
11+
INSERT INTO `book` (`isbn`, `title`, `author_id`, `id`) VALUES ("001-OG", "Carrie", 2, 4);

0 commit comments

Comments
 (0)