Skip to content

Commit ef3e208

Browse files
committed
Records as embeddable and DTO
1 parent 37b7a59 commit ef3e208

File tree

10 files changed

+267
-0
lines changed

10 files changed

+267
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
**[Avoid Spring Redundant `save()` Call](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootRedundantSave)**
2+
3+
**Description:** This application is an example when calling `save()` for an entity is redundant (not necessary).
4+
5+
**Key points:**
6+
- at flush time, Hibernate relies on *dirty checking* mechanism to determine the potential modifications in entities
7+
- for each modification, Hibernate automatically triggers the corresponding `UPDATE` statement without the need to explicitly call the `save()` method
8+
- behind the scene, this redundancy (calling `save()` when is not necessarily) doesn't affect the number of triggered queries, but it implies a performance penalty in the underlying Hibernate processes
9+
10+
-----------------------------------------------------------------------------------------------------------------------
11+
<table>
12+
<tr><td><b>If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"</b></td><td><b>If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.</b></td></tr>
13+
<tr><td>
14+
<a href="https://www.apress.com/us/book/9781484256251"><p align="left"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Spring%20Boot%20Persistence%20Best%20Practices.jpg" height="500" width="450"/></p></a>
15+
</td><td>
16+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="right"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="500" width="450"/></p></a>
17+
</td></tr></table>
18+
19+
-----------------------------------------------------------------------------------------------------------------------
20+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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>HibernateSpringBootRecordAndEmbeddables</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>HibernateSpringBootRecordAndEmbeddables</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>3.0.2</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>19</java.version>
25+
<maven.compiler.source>19</maven.compiler.source>
26+
<maven.compiler.target>19</maven.compiler.target>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
<exclusions>
34+
<exclusion>
35+
<groupId>org.hibernate</groupId>
36+
<artifactId>hibernate-entitymanager</artifactId>
37+
</exclusion>
38+
<exclusion>
39+
<groupId>org.hibernate</groupId>
40+
<artifactId>hibernate-core</artifactId>
41+
</exclusion>
42+
</exclusions>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.hibernate.orm</groupId>
46+
<artifactId>hibernate-core</artifactId>
47+
<version>6.2.0.CR2</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-jdbc</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-web</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.mysql</groupId>
59+
<artifactId>mysql-connector-j</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-test</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
</dependencies>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.bookstore;
2+
3+
import com.bookstore.dto.AuthorDto;
4+
import java.util.List;
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+
List<AuthorDto> authors = bookstoreService.fetchByGenre();
29+
30+
System.out.println(authors);
31+
};
32+
}
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.bookstore.dto;
2+
3+
import com.bookstore.embeddable.Contact;
4+
5+
public record AuthorDto(String name, int age, Contact contact) {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.bookstore.embeddable;
2+
3+
import jakarta.persistence.Embeddable;
4+
5+
@Embeddable
6+
public record Contact(String email, String twitter, String phone) {}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.bookstore.entity;
2+
3+
import com.bookstore.embeddable.Contact;
4+
import jakarta.persistence.Embedded;
5+
import jakarta.persistence.Entity;
6+
import jakarta.persistence.GeneratedValue;
7+
import jakarta.persistence.GenerationType;
8+
import jakarta.persistence.Id;
9+
import java.io.Serializable;
10+
11+
@Entity
12+
public class Author implements Serializable {
13+
14+
private static final long serialVersionUID = 1L;
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private Long id;
19+
20+
@Embedded
21+
private Contact contact;
22+
23+
private int age;
24+
private String name;
25+
private String genre;
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public String getGenre() {
44+
return genre;
45+
}
46+
47+
public void setGenre(String genre) {
48+
this.genre = genre;
49+
}
50+
51+
public int getAge() {
52+
return age;
53+
}
54+
55+
public void setAge(int age) {
56+
this.age = age;
57+
}
58+
59+
public Contact getContact() {
60+
return contact;
61+
}
62+
63+
public void setContact(Contact contact) {
64+
this.contact = contact;
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return "Author{" + "id=" + id + ", age=" + age
70+
+ ", name=" + name + ", genre=" + genre + '}';
71+
}
72+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.dto.AuthorDto;
4+
import java.util.List;
5+
import com.bookstore.entity.Author;
6+
import org.springframework.data.jpa.repository.JpaRepository;
7+
import org.springframework.stereotype.Repository;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@Repository
11+
public interface AuthorRepository extends JpaRepository<Author, Long> {
12+
13+
@Transactional(readOnly = true)
14+
List<AuthorDto> findByGenre(String genre);
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bookstore.service;
2+
3+
import com.bookstore.dto.AuthorDto;
4+
import java.util.List;
5+
import com.bookstore.repository.AuthorRepository;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class BookstoreService {
10+
11+
private final AuthorRepository authorRepository;
12+
13+
public BookstoreService(AuthorRepository authorRepository) {
14+
this.authorRepository = authorRepository;
15+
}
16+
17+
public List<AuthorDto> fetchByGenre() {
18+
19+
return authorRepository.findByGenre("Anthology");
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredb?createDatabaseIfNotExist=true
2+
spring.datasource.username=root
3+
spring.datasource.password=root
4+
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.show-sql=true
7+
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
9+
10+
spring.jpa.defer-datasource-initialization=true
11+
spring.sql.init.mode=always
12+
13+
spring.jpa.open-in-view=false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
insert into author (age, name, genre, email, twitter, phone, id) values (23, "Mark Janel", "Anthology", "[email protected]", "@markjanel", "+40198503", 1);
2+
insert into author (age, name, genre, email, twitter, phone, id) values (43, "Olivia Goy", "Horror", "[email protected]", "@oliviagoy", "+40444933", 2);
3+
insert into author (age, name, genre, email, twitter, phone, id) values (51, "Quartis Young", "Anthology", "[email protected]", "@quartisyoung", "+40909876", 3);
4+
insert into author (age, name, genre, email, twitter, phone, id) values (34, "Joana Nimar", "History", "[email protected]", "@joananimar", "+40765433", 4);
5+
insert into author (age, name, genre, email, twitter, phone, id) values (38, "Alicia Tom", "Anthology", "[email protected]", "@aliciatom", "+40909888", 5);
6+
insert into author (age, name, genre, email, twitter, phone, id) values (56, "Katy Loin", "Anthology", "[email protected]", "@katyloin", "+40294883", 6);

0 commit comments

Comments
 (0)