-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJPAUnitTestCase.java
326 lines (257 loc) · 9.78 KB
/
JPAUnitTestCase.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package org.hibernate.bugs;
import org.hibernate.Hibernate;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.*;
/**
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API.
*/
public class JPAUnitTestCase {
private EntityManagerFactory entityManagerFactory;
@Before
public void init() {
entityManagerFactory = Persistence.createEntityManagerFactory("templatePU");
createSmurfs();
}
@After
public void destroy() {
entityManagerFactory.close();
}
/**
* Simple Join does not fetch joined attribute.
* Children are complete.
* <p>
* JPQL.
*/
@Test
public void simpleJoinDoesNotFetchAttributeJpql() {
inTransaction(em -> {
final TypedQuery<Parent> query = em.createQuery(
"SELECT p FROM Parent p JOIN p.children c WHERE c.name = 'Baby Smurf'",
Parent.class
);
final Parent parent = query.getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isFalse();
assertThat(parent.children).hasSize(3);
});
}
/**
* Simple Join does not fetch joined attribute.
* Children are complete.
* <p>
* Criteria API.
*/
@Test
public void selectJoinWithSimpleConditionCriteriaApi() {
inTransaction(em -> {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Parent> query = cb.createQuery(Parent.class);
final Root<Parent> root = query.from(Parent.class);
final Join<Object, Object> join = root.join("children");
query.where(cb.equal(join.get("name"), "Baby Smurf"));
final Parent parent = em.createQuery(query).getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isFalse();
assertThat(parent.children).hasSize(3);
});
}
/**
* This not a fetch join as described in the specification.
* <p>
* It does limit the returned parent, but also limits the contained children.
* Since it is a fetch join the children list is initialized.
* <p>
* By this usage a FETCH JOIN is a JOIN.
*/
@Test
public void fetchJoinUsedInCoditionLimitsResultsJpql() {
inTransaction(em -> {
final TypedQuery<Parent> query = em.createQuery(
"SELECT p FROM Parent p JOIN FETCH p.children c WHERE c.name = 'Baby Smurf'",
Parent.class
);
final Parent parent = query.getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isTrue();
assertThat(parent.children).hasSize(1);
});
}
/**
* One actually can use a fetch join as a join in the Criteria API if one casts the {@link javax.persistence.criteria.Fetch} to {@link Join}.
* <p>
* This is highly brittle since it depends on implementation details.
* <p>
* It does limit the returned parent, but also limits the contained children.
* Since it is a fetch join the children list is initialized.
*/
@Test
public void fetchJoinUsedInCoditionHackedLimitsResultsCriteriaApi() {
inTransaction(em -> {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Parent> query = cb.createQuery(Parent.class);
final Root<Parent> root = query.from(Parent.class);
final Join<Object, Object> fetchJoin = (Join<Object, Object>) root.fetch("children"); // <--- evil hack because the implementation of fetch join happens to be a join.
query.where(cb.equal(fetchJoin.get("name"), "Baby Smurf"));
final Parent parent = em.createQuery(query).getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isTrue();
assertThat(parent.children).hasSize(1);
assertThat(root.getJoins()).hasSize(0); // <--- Look Ma, no joins
assertThat(root.getFetches()).hasSize(1);
});
}
/**
* One cannot use a path expression to silently reuse a fetch with Criteria API. The test fails when executed.
*/
@Test
@Ignore
public void fetchJoinUsedInCoditionFailsCriterApi() {
inTransaction(em -> {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Parent> query = cb.createQuery(Parent.class);
final Root<Parent> root = query.from(Parent.class);
root.fetch("children");
query.where(cb.equal(root.get("children").get("name"), "Baby Smurf"));
final Parent parent = em.createQuery(query).getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isFalse();
assertThat(Hibernate.isInitialized(parent.favoriteChild)).isTrue();
assertThat(parent.children).hasSize(3);
});
}
/**
* This is what the specification suggests to use if one wants to use a joined attribute in a condition AND wants to fetch that join as well:
* Join fetch PLUS a JOIN initializes the collection and loads the children.
*/
@Test
public void joinFetchPlusJoinWithConditionFetchesAllChildrenAndFiltersParentByChildrenJpql() {
inTransaction(em -> {
final TypedQuery<Parent> query = em.createQuery(
"SELECT p FROM Parent p JOIN FETCH p.children JOIN p.children c WHERE c.name = 'Baby Smurf'",
Parent.class
);
final Parent parent = query.getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isTrue();
assertThat(parent.children).hasSize(3);
});
}
/**
* This is what the specification suggests to use if one wants to use a joined attribute in a condition AND wants to fetch that join as well:
* Join fetch PLUS a JOIN initializes the collection and loads the children.
* <p>
* But when queried with {@code getResultList()} duplicates are returned.
*/
@Test
public void joinFetchPlusJoinWithConditionFetchesAllChildrenAndFiltersParentByChildrenResultListJpql() {
inTransaction(em -> {
final TypedQuery<Parent> query = em.createQuery(
"SELECT p FROM Parent p JOIN FETCH p.children JOIN p.children c WHERE c.name = 'Baby Smurf'",
Parent.class
);
final List<Parent> parents = query.getResultList();
assertThat(parents).hasSize(3);
assertThat(parents.get(0)).isSameAs(parents.get(1)).isSameAs(parents.get(1));
Parent parent = parents.get(0);
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isTrue();
assertThat(parent.children).hasSize(3);
});
}
/**
* This is what the specification suggests to use if one wants to use a joined attribute in a condition AND wants to fetch that join as well:
* Join fetch PLUS a JOIN initializes the collection and loads the children.
*/
@Test
public void joinFetchPlusJoinWithConditionFetchesAllChildrenAndFiltersParentByChildrenJpqlCriteriaApi() {
inTransaction(em -> {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Parent> query = cb.createQuery(Parent.class);
final Root<Parent> root = query.from(Parent.class);
root.fetch("children");
final Join<Object, Object> join = root.join("children");
query.where(cb.equal(join.get("name"), "Baby Smurf"));
final Parent parent = em.createQuery(query).getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isTrue();
assertThat(parent.children).hasSize(3);
});
}
/**
* For * to One relations reusing a Fetch Join does not alter the result in a surprising way.
*/
@Test
public void fetchJoinReusedByPathExpressionForOneToOneJpqlMatch() {
inTransaction(em -> {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Parent> query = cb.createQuery(Parent.class);
final Root<Parent> root = query.from(Parent.class);
root.fetch("favoriteChild");
query.where(cb.equal(root.get("favoriteChild").get("name"), "Baby Smurf"));
final Parent parent = em.createQuery(query).getSingleResult();
assertThat(parent).isNotNull();
assertThat(Hibernate.isInitialized(parent.children)).isFalse();
assertThat(Hibernate.isInitialized(parent.favoriteChild)).isTrue();
assertThat(parent.children).hasSize(3);
});
}
/**
* For * to One relations reusing a Fetch Join does not alter the result in a surprising way.
*/
@Test
public void fetchJoinReusedByPathExpressionForOneToOneJpqlNoMatch() {
inTransaction(em -> {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Parent> query = cb.createQuery(Parent.class);
final Root<Parent> root = query.from(Parent.class);
root.fetch("favoriteChild", JoinType.LEFT);
query.where(cb.equal(root.get("favoriteChild").get("name"), "xxx"));
final List<Parent> parents = em.createQuery(query).getResultList();
assertThat(parents).isEmpty();
});
}
private void createSmurfs() {
inTransaction(em -> {
final Parent p = new Parent();
p.name = "Papa Smurf";
p.children.add(new Child());
p.children.get(0).name = "Baby Smurf";
p.children.add(new Child());
p.children.get(1).name = "Girl Smurf";
p.children.add(new Child());
p.children.get(2).name = "Boy Smurf";
p.favoriteChild = p.children.get(0);
em.persist(p);
final Parent l = new Parent();
l.name = "Lonely Smurf";
em.persist(l);
});
}
private <T> void inTransaction(Consumer<EntityManager> command) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
try {
command.accept(entityManager);
} finally {
entityManager.getTransaction().commit();
entityManager.close();
}
}
// select with fetch JPQL
// select with join JPQL
//
}