Skip to content

Commit 25aee50

Browse files
committed
Avoid unidirectional @onetomany
1 parent dd62356 commit 25aee50

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

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

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

2828
System.out.println("\n---------------------------------------------");
2929

30-
System.out.println("\nAdd new book to an author ...");
31-
bookstoreService.addNewBook();
30+
System.out.println("\nInsert new book to an author ...");
31+
bookstoreService.insertNewBook();
3232

3333
System.out.println("\n---------------------------------------------");
3434

HibernateSpringBootOneToManyUnidirectinal/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
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public BookstoreService(AuthorRepository authorRepository) {
1818
}
1919

2020
@Transactional
21-
public void addAuthorWithBooks() {
21+
public void insertAuthorWithBooks() {
2222

2323
Author jn = new Author();
2424
jn.setName("Joana Nimar");
@@ -45,9 +45,9 @@ public void addAuthorWithBooks() {
4545
}
4646

4747
@Transactional
48-
public void addNewBook() {
48+
public void insertNewBook() {
4949

50-
Author author = authorRepository.findByName("Joana Nimar");
50+
Author author = authorRepository.fetchByName("Joana Nimar");
5151

5252
Book book = new Book();
5353
book.setIsbn("004-JN");
@@ -61,7 +61,7 @@ public void addNewBook() {
6161
@Transactional
6262
public void deleteLastBook() {
6363

64-
Author author = authorRepository.findByName("Joana Nimar");
64+
Author author = authorRepository.fetchByName("Joana Nimar");
6565
List<Book> books = author.getBooks();
6666

6767
author.removeBook(books.get(books.size() - 1));
@@ -70,7 +70,7 @@ public void deleteLastBook() {
7070
@Transactional
7171
public void deleteFirstBook() {
7272

73-
Author author = authorRepository.findByName("Joana Nimar");
73+
Author author = authorRepository.fetchByName("Joana Nimar");
7474
List<Book> books = author.getBooks();
7575

7676
author.removeBook(books.get(0));

0 commit comments

Comments
 (0)