Skip to content

Commit 0395400

Browse files
committed
Spring projections including collections
1 parent ad66d94 commit 0395400

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public ApplicationRunner init() {
4444
System.out.println("\n\nFetch authors with books via array of objects and transform to DTO:");
4545
System.out.println("-----------------------------------------------------------------");
4646
bookstoreService.fetchAuthorsWithBooksViaArrayOfObjectsAndTransformToDto();
47+
48+
System.out.println("\n\nFetch authors with books via JdbcTemplate as DTO:");
49+
System.out.println("-----------------------------------------------------------------");
50+
bookstoreService.fetchAuthorsWithBooksViaJdbcTemplateToDto();
4751
};
4852
}
4953
}

HibernateSpringBootProjectionAndCollections/src/main/java/com/bookstore/controller/BookstoreController.java

+5
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public List<Object[]> fetchAuthorsWithBooksViaArrayOfObjects() {
4646
public List<com.bookstore.transform.dto.AuthorDto> fetchAuthorsWithBooksViaArrayOfObjectsAndTransformToDto() {
4747
return bookstoreService.fetchAuthorsWithBooksViaArrayOfObjectsAndTransformToDto();
4848
}
49+
50+
@GetMapping("/authorsAndbooks/7")
51+
public List<com.bookstore.jdbcTemplate.dto.AuthorDto> fetchAuthorsWithBooksViaJdbcTemplateToDto() {
52+
return bookstoreService.fetchAuthorsWithBooksViaJdbcTemplateToDto();
53+
}
4954
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.bookstore.jdbcTemplate.dto;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class AuthorDto implements Serializable {
8+
9+
private static final long serialVersionUID = 1L;
10+
11+
private Long authorId;
12+
private String name;
13+
private String genre;
14+
15+
private List<BookDto> books = new ArrayList<>();
16+
17+
public AuthorDto() {
18+
}
19+
20+
public AuthorDto(Long authorId, String name, String genre) {
21+
this.authorId = authorId;
22+
this.name = name;
23+
this.genre = genre;
24+
}
25+
26+
public Long getId() {
27+
return authorId;
28+
}
29+
30+
public void setId(Long authorId) {
31+
this.authorId = authorId;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public String getGenre() {
43+
return genre;
44+
}
45+
46+
public void setGenre(String genre) {
47+
this.genre = genre;
48+
}
49+
50+
public List<BookDto> getBooks() {
51+
return books;
52+
}
53+
54+
public void setBooks(List<BookDto> books) {
55+
this.books = books;
56+
}
57+
58+
public void addBook(BookDto book) {
59+
books.add(book);
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "AuthorDto{" + "authorId=" + authorId + ", name=" + name + ", genre=" + genre + '}';
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.bookstore.jdbcTemplate.dto;
2+
3+
import java.sql.ResultSet;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import org.springframework.jdbc.core.JdbcTemplate;
9+
import org.springframework.stereotype.Repository;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@Repository
13+
@Transactional(readOnly = true)
14+
public class AuthorExtractor {
15+
16+
private final JdbcTemplate jdbcTemplate;
17+
18+
public AuthorExtractor(JdbcTemplate jdbcTemplate) {
19+
this.jdbcTemplate = jdbcTemplate;
20+
}
21+
22+
public List<AuthorDto> extract() {
23+
24+
String sql = "SELECT a.id, a.name, a.genre, b.id, b.title "
25+
+ "FROM author a INNER JOIN book b ON a.id = b.author_id";
26+
27+
List<AuthorDto> result = jdbcTemplate.query(sql, (ResultSet rs) -> {
28+
29+
final Map<Long, AuthorDto> authorsMap = new HashMap<>();
30+
while (rs.next()) {
31+
Long authorId = (rs.getLong("id"));
32+
AuthorDto author = authorsMap.get(authorId);
33+
if (author == null) {
34+
author = new AuthorDto();
35+
author.setId(rs.getLong("id"));
36+
author.setName(rs.getString("name"));
37+
author.setGenre(rs.getString("genre"));
38+
}
39+
40+
BookDto book = new BookDto();
41+
book.setId(rs.getLong("id"));
42+
book.setTitle(rs.getString("title"));
43+
44+
author.addBook(book);
45+
authorsMap.putIfAbsent(author.getId(), author);
46+
}
47+
48+
return new ArrayList<>(authorsMap.values());
49+
});
50+
51+
return result;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.bookstore.jdbcTemplate.dto;
2+
3+
import java.io.Serializable;
4+
5+
public class BookDto implements Serializable {
6+
7+
private static final long serialVersionUID = 1L;
8+
9+
private Long bookId;
10+
private String title;
11+
12+
public BookDto() {
13+
}
14+
15+
public BookDto(Long bookId, String title) {
16+
this.bookId = bookId;
17+
this.title = title;
18+
}
19+
20+
public Long getId() {
21+
return bookId;
22+
}
23+
24+
public void setId(Long bookId) {
25+
this.bookId = bookId;
26+
}
27+
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
public void setTitle(String title) {
33+
this.title = title;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "BookDto{" + "bookId="
39+
+ bookId + ", title=" + title + '}';
40+
}
41+
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bookstore.service;
22

3+
import com.bookstore.jdbcTemplate.dto.AuthorExtractor;
34
import com.bookstore.spring.dto.AuthorDto;
45
import com.bookstore.spring.dto.SimpleAuthorDto;
56
import com.bookstore.repository.AuthorRepository;
@@ -21,17 +22,20 @@ public class BookstoreService {
2122

2223
private final AuthorRepository authorRepository;
2324
private final AuthorTransformer authorTransformer;
25+
private final AuthorExtractor authorExtractor;
2426

2527
@PersistenceContext
2628
private final EntityManager entityManager;
2729

2830
public BookstoreService(AuthorRepository authorRepository,
2931
AuthorTransformer authorTransformer,
32+
AuthorExtractor authorExtractor,
3033
EntityManager entityManager) {
3134

3235
this.authorRepository = authorRepository;
3336
this.entityManager = entityManager;
3437
this.authorTransformer = authorTransformer;
38+
this.authorExtractor = authorExtractor;
3539
}
3640

3741
@Transactional(readOnly = true)
@@ -119,6 +123,22 @@ public List<com.bookstore.transform.dto.AuthorDto> fetchAuthorsWithBooksViaArray
119123
return authorsDto;
120124
}
121125

126+
@Transactional(readOnly = true)
127+
public List<com.bookstore.jdbcTemplate.dto.AuthorDto> fetchAuthorsWithBooksViaJdbcTemplateToDto() {
128+
129+
List<com.bookstore.jdbcTemplate.dto.AuthorDto> authors = authorExtractor.extract();
130+
131+
System.out.println("\nResult set:");
132+
authors.forEach(a -> {
133+
System.out.println("\n\n" + a.getName() + ", " + a.getGenre());
134+
a.getBooks().forEach(b -> System.out.print(b.getTitle() + ", "));
135+
});
136+
137+
briefOverviewOfPersistentContextContent();
138+
139+
return authors;
140+
}
141+
122142
private void briefOverviewOfPersistentContextContent() {
123143
org.hibernate.engine.spi.PersistenceContext persistenceContext = getPersistenceContext();
124144

0 commit comments

Comments
 (0)