Skip to content

Commit a4cc5dd

Browse files
committed
DTO via a custom ResultTransformer
1 parent 219b43f commit a4cc5dd

File tree

12 files changed

+519
-0
lines changed

12 files changed

+519
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
**[How To Fetch DTO Via `ResultTransformer` and JPQL](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootDtoResultTransformerJpql)**
2+
3+
**Description:** Fetching more data than needed is prone to performance penalties. Using DTO allows us to extract only the needed data. In this application we rely on Hibernate, `ResultTransformer` and JPQL.
4+
5+
**Key points:**
6+
- use `AliasToBeanConstructorResultTransformer` for DTO without setters, with constructor
7+
- use `Transformers.aliasToBean()` for DTO with setters
8+
- use `EntityManager.createQuery()` and `unwrap(org.hibernate.query.Query.class)`
9+
- starting with Hibernate 5.2, `ResultTransformer` is deprecated, but until a replacement will be available (in Hibernate 6.0) it can be used ([read further](https://discourse.hibernate.org/t/hibernate-resulttransformer-is-deprecated-what-to-use-instead/232))
10+
- for using Spring Data Projections check this [item](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootDtoViaProjections)
11+
12+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="center"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="410" width="350"/></p></a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>HibernateSpringBootDtoCustomResultTransformer</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>HibernateSpringBootDtoCustomResultTransformer</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-jdbc</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>mysql</groupId>
42+
<artifactId>mysql-connector-java</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bookstore;
2+
3+
import com.bookstore.service.BookstoreService;
4+
import org.springframework.boot.ApplicationRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class MainApplication {
11+
12+
private final BookstoreService bookstoreService;
13+
14+
public MainApplication(BookstoreService bookstoreService) {
15+
this.bookstoreService = bookstoreService;
16+
}
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(MainApplication.class, args);
20+
}
21+
22+
@Bean
23+
public ApplicationRunner init() {
24+
return args -> {
25+
bookstoreService.fetchAuthorWithBook();
26+
};
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.bookstore.dao;
2+
3+
import com.bookstore.dto.AuthorDto;
4+
import com.bookstore.transformer.AuthorBookTransformer;
5+
import java.util.List;
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.PersistenceContext;
8+
import javax.persistence.Query;
9+
import org.springframework.stereotype.Repository;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@Repository
13+
public class Dao {
14+
15+
@PersistenceContext
16+
private EntityManager entityManager;
17+
18+
@Transactional(readOnly = true)
19+
public List<AuthorDto> fetchAuthorWithBook() {
20+
Query query = entityManager
21+
.createNativeQuery(
22+
"SELECT a.id AS author_id, a.name AS name, a.age AS age, "
23+
+ "b.id AS book_id, b.title AS title "
24+
+ "FROM author a JOIN book b ON a.id=b.author_id")
25+
.unwrap(org.hibernate.query.NativeQuery.class)
26+
.setResultTransformer(new AuthorBookTransformer());
27+
28+
List<AuthorDto> authors = query.getResultList();
29+
30+
return authors;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.bookstore.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 int age;
14+
15+
private List<BookDto> books = new ArrayList<>();
16+
17+
public AuthorDto() {
18+
}
19+
20+
public AuthorDto(Long authorId, String name, int age) {
21+
this.authorId = authorId;
22+
this.name = name;
23+
this.age = age;
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 int getAge() {
43+
return age;
44+
}
45+
46+
public void setAge(int age) {
47+
this.age = age;
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 + ", age=" + age + '}';
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.bookstore.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+
public BookDto(Long bookId, String title) {
15+
this.bookId = bookId;
16+
this.title = title;
17+
}
18+
19+
public Long getId() {
20+
return bookId;
21+
}
22+
23+
public void setId(Long bookId) {
24+
this.bookId = bookId;
25+
}
26+
27+
public String getTitle() {
28+
return title;
29+
}
30+
31+
public void setTitle(String title) {
32+
this.title = title;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "BookDto{" + "bookId="
38+
+ bookId + ", title=" + title + '}';
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import javax.persistence.CascadeType;
8+
import javax.persistence.Entity;
9+
import javax.persistence.GeneratedValue;
10+
import javax.persistence.GenerationType;
11+
import javax.persistence.Id;
12+
import javax.persistence.OneToMany;
13+
14+
@Entity
15+
public class Author implements Serializable {
16+
17+
private static final long serialVersionUID = 1L;
18+
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.IDENTITY)
21+
private Long id;
22+
23+
private String name;
24+
private String genre;
25+
private int age;
26+
27+
@OneToMany(cascade = CascadeType.ALL,
28+
mappedBy = "author", orphanRemoval = true)
29+
private List<Book> books = new ArrayList<>();
30+
31+
public void addBook(Book book) {
32+
this.books.add(book);
33+
book.setAuthor(this);
34+
}
35+
36+
public void removeBook(Book book) {
37+
book.setAuthor(null);
38+
this.books.remove(book);
39+
}
40+
41+
public void removeBooks() {
42+
Iterator<Book> iterator = this.books.iterator();
43+
44+
while (iterator.hasNext()) {
45+
Book book = iterator.next();
46+
47+
book.setAuthor(null);
48+
iterator.remove();
49+
}
50+
}
51+
52+
public Long getId() {
53+
return id;
54+
}
55+
56+
public void setId(Long id) {
57+
this.id = id;
58+
}
59+
60+
public String getName() {
61+
return name;
62+
}
63+
64+
public void setName(String name) {
65+
this.name = name;
66+
}
67+
68+
public String getGenre() {
69+
return genre;
70+
}
71+
72+
public void setGenre(String genre) {
73+
this.genre = genre;
74+
}
75+
76+
public int getAge() {
77+
return age;
78+
}
79+
80+
public void setAge(int age) {
81+
this.age = age;
82+
}
83+
84+
public List<Book> getBooks() {
85+
return books;
86+
}
87+
88+
public void setBooks(List<Book> books) {
89+
this.books = books;
90+
}
91+
92+
@Override
93+
public String toString() {
94+
return "Author{" + "id=" + id + ", name=" + name
95+
+ ", genre=" + genre + ", age=" + age + '}';
96+
}
97+
98+
}

0 commit comments

Comments
 (0)