Skip to content

Commit 12d98f4

Browse files
committed
Join Fetch
1 parent 85b151d commit 12d98f4

19 files changed

+294
-264
lines changed
Loading

HibernateSpringBootJoinFetch/pom.xml

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.jpa</groupId>
7-
<artifactId>jpa</artifactId>
7+
<artifactId>HibernateSpringBootJoinFetch</artifactId>
88
<version>1.0</version>
99
<packaging>jar</packaging>
1010

11-
<name>jpa</name>
11+
<name>HibernateSpringBootJoinFetch</name>
1212
<description>JPA project for Spring Boot</description>
1313

1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.0.5.RELEASE</version>
17+
<version>2.1.4.RELEASE</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

@@ -57,6 +57,4 @@
5757
</plugin>
5858
</plugins>
5959
</build>
60-
61-
6260
</project>
-15.6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bookstore;
2+
3+
import com.bookstore.entity.Author;
4+
import com.bookstore.entity.Book;
5+
import com.bookstore.service.BookstoreService;
6+
import org.springframework.boot.ApplicationRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
@SpringBootApplication
12+
public class MainApplication {
13+
14+
private final BookstoreService bookstoreService;
15+
16+
public MainApplication(BookstoreService bookstoreService) {
17+
this.bookstoreService = bookstoreService;
18+
}
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(MainApplication.class, args);
22+
}
23+
24+
@Bean
25+
public ApplicationRunner init() {
26+
return args -> {
27+
28+
Author author = bookstoreService.fetchAuthorWithBooksByName();
29+
System.out.println("\nAuthor name: " + author.getName() + " Books: " + author.getBooks() + "\n");
30+
31+
Book book = bookstoreService.fetchBookWithAuthorByIsbn();
32+
System.out.println("\nTitle: " + book.getTitle() + " author: " + book.getAuthor() + "\n");
33+
34+
};
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import javax.persistence.CascadeType;
7+
import javax.persistence.Entity;
8+
import javax.persistence.Id;
9+
import javax.persistence.OneToMany;
10+
11+
@Entity
12+
public class Author implements Serializable {
13+
14+
private static final long serialVersionUID = 1L;
15+
16+
@Id
17+
private Long id;
18+
19+
private String name;
20+
private String genre;
21+
private int age;
22+
23+
@OneToMany(cascade = CascadeType.ALL,
24+
mappedBy = "author", orphanRemoval = true)
25+
private List<Book> books = new ArrayList<>();
26+
27+
public void addBook(Book book) {
28+
this.books.add(book);
29+
book.setAuthor(this);
30+
}
31+
32+
public void removeBook(Book book) {
33+
book.setAuthor(null);
34+
this.books.remove(book);
35+
}
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
public String getGenre() {
54+
return genre;
55+
}
56+
57+
public void setGenre(String genre) {
58+
this.genre = genre;
59+
}
60+
61+
public int getAge() {
62+
return age;
63+
}
64+
65+
public void setAge(int age) {
66+
this.age = age;
67+
}
68+
69+
public List<Book> getBooks() {
70+
return books;
71+
}
72+
73+
public void setBooks(List<Book> books) {
74+
this.books = books;
75+
}
76+
77+
@Override
78+
public String toString() {
79+
return "Author{" + "id=" + id + ", name=" + name
80+
+ ", genre=" + genre + ", age=" + age + '}';
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.FetchType;
6+
import javax.persistence.Id;
7+
import javax.persistence.JoinColumn;
8+
import javax.persistence.ManyToOne;
9+
10+
@Entity
11+
public class Book implements Serializable {
12+
13+
private static final long serialVersionUID = 1L;
14+
15+
@Id
16+
private Long id;
17+
18+
private String title;
19+
private String isbn;
20+
21+
@ManyToOne(fetch = FetchType.LAZY)
22+
@JoinColumn(name = "author_id")
23+
private Author author;
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getTitle() {
34+
return title;
35+
}
36+
37+
public void setTitle(String title) {
38+
this.title = title;
39+
}
40+
41+
public String getIsbn() {
42+
return isbn;
43+
}
44+
45+
public void setIsbn(String isbn) {
46+
this.isbn = isbn;
47+
}
48+
49+
public Author getAuthor() {
50+
return author;
51+
}
52+
53+
public void setAuthor(Author author) {
54+
this.author = author;
55+
}
56+
57+
@Override
58+
public boolean equals(Object obj) {
59+
if (this == obj) {
60+
return true;
61+
}
62+
if (!(obj instanceof Book)) {
63+
return false;
64+
}
65+
return id != null && id.equals(((Book) obj).id);
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return 2021;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return "Book{" + "id=" + id + ", title=" + title + ", isbn=" + isbn + '}';
76+
}
77+
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Author;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.stereotype.Repository;
7+
import org.springframework.transaction.annotation.Transactional;
8+
9+
@Repository
10+
public interface AuthorRepository extends JpaRepository<Author, Long> {
11+
12+
@Transactional(readOnly = true)
13+
@Query(value = "SELECT a FROM Author a JOIN FETCH a.books WHERE a.name = ?1")
14+
Author fetchAuthorWithBooksByName(String name);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Book;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.stereotype.Repository;
7+
import org.springframework.transaction.annotation.Transactional;
8+
9+
@Repository
10+
public interface BookRepository extends JpaRepository<Book, Long> {
11+
12+
@Transactional(readOnly = true)
13+
@Query(value = "SELECT b FROM Book b JOIN FETCH b.author WHERE b.isbn = ?1")
14+
Book fetchBookWithAuthorByIsbn(String isbn);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.bookstore.service;
2+
3+
import com.bookstore.repository.AuthorRepository;
4+
import com.bookstore.repository.BookRepository;
5+
import com.bookstore.entity.Author;
6+
import com.bookstore.entity.Book;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class BookstoreService {
11+
12+
private final AuthorRepository authorRepository;
13+
private final BookRepository bookRepository;
14+
15+
public BookstoreService(AuthorRepository authorRepository,
16+
BookRepository bookRepository) {
17+
this.authorRepository = authorRepository;
18+
this.bookRepository = bookRepository;
19+
}
20+
21+
public Author fetchAuthorWithBooksByName() {
22+
23+
return authorRepository.fetchAuthorWithBooksByName("Joana Nimar");
24+
}
25+
26+
public Book fetchBookWithAuthorByIsbn() {
27+
28+
return bookRepository.fetchBookWithAuthorByIsbn("002-JN");
29+
}
30+
}

HibernateSpringBootJoinFetch/src/main/java/com/jpa/Category.java

-64
This file was deleted.

HibernateSpringBootJoinFetch/src/main/java/com/jpa/CategoryRepository.java

-14
This file was deleted.

0 commit comments

Comments
 (0)