-
Notifications
You must be signed in to change notification settings - Fork 10
/
HibernateJUnit4Test.java
165 lines (127 loc) · 6.66 KB
/
HibernateJUnit4Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2020-2021 the original author or authors.
*/
package org.quickperf.sql;
import football.dto.PlayerWithTeamName;
import football.entity.Player;
import net.ttddyy.dsproxy.support.ProxyDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quickperf.annotation.DisableGlobalAnnotations;
import org.quickperf.junit4.QuickPerfJUnitRunner;
import org.quickperf.sql.annotation.ExpectSelect;
import org.quickperf.sql.config.QuickPerfSqlDataSourceBuilder;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.sql.DataSource;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.quickperf.sql.config.HibernateEntityManagerBuilder.anHibernateEntityManager;
import static org.quickperf.sql.config.TestDataSourceBuilder.aDataSource;
/* At the beginning of each test, a SQL script defined in import.sql file is executed. */
@RunWith(QuickPerfJUnitRunner.class)
public class HibernateJUnit4Test {
/* FIRST TYPE OF N+1 SELECT
We expect to retrieve all the players from the database with one SELECT statement.
This is verified with the help of an @ExpectSelect(1) QuickPerf annotation added on
test method.
Let's execute this test method. This test is failing!
In the bottom of the error report, the SQL executions are displayed. We have one
SELECT statement on Player table, and two more on Team table. For each player a
SELECT statement is executed to retrieve his team. We have a N+1 select issue as
suggested by the error report: "Perhaps you are facing a N+1 select issue".
The error report proposes several ways of fixing an N+1 SELECT.
In this case, in the Player entity, let's specify a lazy fetch type for the
relationship between the players and their team:
@ManyToOne(targetEntity = Team.class, fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;
If you run the test with this modification, it will be green!
*/
@ExpectSelect(1)
@DisableGlobalAnnotations // Global annotations are explained with the
// second test method. Global annotations are
// disabled here to explain one thing at a time.
@Test
public void should_find_all_players() {
TypedQuery<Player> fromPlayer
= entityManager.createQuery("FROM Player", Player.class);
List<Player> players = fromPlayer.getResultList();
assertThat(players).hasSize(2);
}
// In the previous example, the outcome of the N+1 select was to have additional
// SELECT statements on Team table. These additional SELECT statements are the same
// apart from the id value of the Team table.
//
// With QuickPerf, you can easily detect that the same SELECT statements with different
// values are executed, allowing to catch N+1 selects. We are going to see this in the
// next example.
//-------------------------------------------------------------------------------------
/* SECOND TYPE OF N+1 SELECT
This test method is not annotated with a QuickPerf annotation. However, it will
fail because a N+1 select is detected from a global annotation defined in
org.quickperf.QuickPerfConfiguration class: disableSameSelectTypesWithDifferentParams
Global apply on each test method.
This comes from the following Java code:
List<PlayerWithTeamName> playersWithTeamName =
players
.stream()
.map(player -> new PlayerWithTeamName(
player.getFirstName()
, player.getLastName()
, player.getTeam().getName()
)
)
.collect(Collectors.toList());
Each time the getName() method is called, a SELECT... FROM Team statement is sent to
the database.
This type of N+1 SELECT can't be solved by configuring a LAZY fetch type.
To fix this N+1 SELECT, you can use JOIN FETCH or a LEFT JOIN FETCH:
TypedQuery<Player> fromPlayer
= entityManager.createQuery("FROM Player p LEFT JOIN FETCH p.team", Player.class);TypedQuery<Player> fromPlayer
These solutions are suggested by the QuickPerf error report.
Global annotations apply on each test. In the case where you are developing a new feature,
perhaps with the help of TDD, your test may fail because the business property is
un-respected but also because some performance properties checked by global annotations
are un-respected. In order to do one step at a time, you can temporarily disable global
annotations by applying @FunctionalIteration or @DisableQuickPerf or @DisableGlobalAnnotations
at method level.
*/
//@FunctionalIteration //Uncomment this line to temporarily disable QuickPerf features
@Test
public void should_find_all_players_with_their_team_name() {
TypedQuery<Player> fromPlayer
= entityManager.createQuery("FROM Player", Player.class);
List<Player> players = fromPlayer.getResultList();
List<PlayerWithTeamName> playersWithTeamName =
players
.stream()
.map(player -> new PlayerWithTeamName(
player.getFirstName()
, player.getLastName()
, player.getTeam().getName()
)
)
.collect(Collectors.toList());
assertThat(playersWithTeamName).hasSize(2);
}
//-------------------------------------------------------------------------------------
private EntityManager entityManager;
{
DataSource dataSource = aDataSource().build();
// A data source proxy is built to allow QuickPerf to intercept the SQL statements
ProxyDataSource proxyDataSource = QuickPerfSqlDataSourceBuilder.aDataSourceBuilder()
.buildProxy(dataSource);
entityManager = anHibernateEntityManager(proxyDataSource);
}
}