Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 4847bc1

Browse files
committed
Add new tutorial example
1 parent 4f3bcfb commit 4847bc1

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

performance-optimization/pom.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>jpa-hibernate-tutorial</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>performance-optimization</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.bobocode</groupId>
17+
<artifactId>jpa-hibernate-tutorial-util</artifactId>
18+
<version>1.0-SNAPSHOT</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.dto.AccountProjection;
4+
import com.bobocode.model.Account;
5+
import com.bobocode.util.JpaUtil;
6+
import com.bobocode.util.TestDataGenerator;
7+
8+
import static com.bobocode.util.JpaUtil.performReturningWithinPersistenceContext;
9+
import static com.bobocode.util.JpaUtil.performWithinPersistenceContext;
10+
11+
/**
12+
* This code example shows how to fetch an entity projection instead of full entity.
13+
*/
14+
public class FetchingDtoProjection {
15+
public static void main(String[] args) {
16+
JpaUtil.init("BasicEntitiesH2");
17+
Account account = TestDataGenerator.generateAccount();
18+
performWithinPersistenceContext(entityManager -> entityManager.persist(account));
19+
20+
AccountProjection accountProjection = fetchAccountProjectionById(account.getId());
21+
System.out.println(accountProjection);
22+
JpaUtil.close();
23+
}
24+
25+
/**
26+
* Fetches {@link AccountProjection} by account id. An {@link AccountProjection} is a data transfer object (DTO) for
27+
* {@link Account}. (It's a short version that stores some account data, but is not manged by Hibernate)
28+
*
29+
* @param id account id
30+
* @return account projection
31+
*/
32+
private static AccountProjection fetchAccountProjectionById(Long id) {
33+
return performReturningWithinPersistenceContext(entityManager ->
34+
entityManager.createQuery("select new com.bobocode.dto.AccountProjection(a.id, a.email) " +
35+
"from Account a where a.id = :id", AccountProjection.class)
36+
.setParameter("id", id)
37+
.getSingleResult());
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.bobocode.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import lombok.ToString;
7+
8+
@AllArgsConstructor
9+
@Getter
10+
@Setter
11+
@ToString
12+
public class AccountProjection {
13+
private Long id;
14+
private String email;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
3+
4+
<persistence-unit name="BasicEntitiesH2">
5+
<class>com.bobocode.model.Account</class>
6+
7+
<properties>
8+
<property name="hibernate.connection.url" value="jdbc:h2:mem:bobocode_db;DB_CLOSE_DELAY=-1"/>
9+
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
10+
<property name="hibernate.connection.username" value="bobouser"/>
11+
<property name="hibernate.connection.password" value="bobopass"/>
12+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
13+
<property name="hibernate.show_sql" value="true"/>
14+
<property name="hibernate.format_sql" value="true"/>
15+
<property name="hibernate.hbm2ddl.auto" value="create"/>
16+
</properties>
17+
</persistence-unit>
18+
19+
</persistence>

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<module>dirty-checking-mechanism</module>
2222
<module>entity-relationships-management</module>
2323
<module>persistence-context</module>
24+
<module>performance-optimization</module>
2425
</modules>
2526

2627
<dependencies>

0 commit comments

Comments
 (0)