Skip to content

Commit 26bd629

Browse files
committed
1 parent 25aee50 commit 26bd629

File tree

5 files changed

+20
-22
lines changed

5 files changed

+20
-22
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public static void main(String[] args) {
2424
@Bean
2525
public ApplicationRunner init() {
2626
return args -> {
27-
System.out.println("\nAdd new book to an author ...");
27+
System.out.println("\nInsert new book to an author ...");
2828
System.out.println("---------------------------------------------");
29-
bookstoreService.addNewBook();
29+
bookstoreService.insertNewBook();
3030
System.out.println("---------------------------------------------");
3131

3232
System.out.println("\nList all books of an author ...");
@@ -46,7 +46,7 @@ public ApplicationRunner init() {
4646

4747
System.out.println("\nFetch a list of books and delete the first book ...");
4848
System.out.println("---------------------------------------------");
49-
bookstoreService.fetchBooksOfAuthorByIdDeleteFirstBook();
49+
bookstoreService.fetchBooksOfAuthorByIdAndDeleteFirstBook();
5050
System.out.println("---------------------------------------------");
5151
};
5252
}

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

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

87
@Repository
9-
public interface AuthorRepository extends JpaRepository<Author, Long> {
10-
11-
@Transactional(readOnly=true)
12-
Author findByName(String name);
8+
public interface AuthorRepository extends JpaRepository<Author, Long> {
139
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
import org.springframework.transaction.annotation.Transactional;
1111

1212
@Repository
13+
@Transactional(readOnly = true)
1314
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
14-
15-
@Transactional(readOnly = true)
15+
1616
@Query("SELECT b FROM Book b WHERE b.author.id = :id")
1717
List<Book> fetchBooksOfAuthorById(Long id);
18-
19-
@Transactional(readOnly = true)
18+
2019
@Query("SELECT b FROM Book b WHERE b.author.id = :id")
2120
Page<Book> fetchPageBooksOfAuthorById(Long id, Pageable pageable);
2221
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public BookstoreService(AuthorRepository authorRepository,
2525
}
2626

2727
@Transactional
28-
public void addNewBook() {
28+
public void insertNewBook() {
2929
Author author = authorRepository.getOne(4L);
3030
// or, less efficient since a SELECT is triggered
3131
// Author author = authorRepository.findByName("Joana Nimar");
@@ -66,7 +66,7 @@ public void fetchBooksOfAuthorByIdAndAddNewBook() {
6666
}
6767

6868
@Transactional
69-
public void fetchBooksOfAuthorByIdDeleteFirstBook() {
69+
public void fetchBooksOfAuthorByIdAndDeleteFirstBook() {
7070
List<Book> books = bookRepository.fetchBooksOfAuthorById(4L);
7171

7272
bookRepository.delete(books.remove(0));
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)