/
OrphanRemovalTest.java
347 lines (288 loc) · 12.8 KB
/
OrphanRemovalTest.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package com.martinandersson.javaee.jpa.mapping.orphanremoval;
import com.martinandersson.javaee.resources.SchemaGenerationStrategy;
import com.martinandersson.javaee.utils.DeploymentBuilder;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
/**
* See:
* <pre>{@code
*
* http://docs.oracle.com/javaee/7/tutorial/persistence-intro001.htm#JEETT00677
*
* }</pre>
*
* Currently, the example provided by the Java EE 7 tutorial and tested in
* {@linkplain #removeFromRelationshipWithOrphanRemoval_entityAlsoRemoved() removeFromRelationshipWithOrphanRemoval_entityAlsoRemoved()}
* does not work on WildFly/Hibernate.<p>
*
* TODO: Report bug.
*
* @author Martin Andersson (webmaster at martinandersson.com)
*/
@RunWith(Arquillian.class)
public class OrphanRemovalTest
{
@Deployment
private static Archive<?> buildDeployment() {
return new DeploymentBuilder(OrphanRemovalTest.class)
.addPersistenceXMLFile(SchemaGenerationStrategy.UPDATE)
.addTestPackage()
.build();
}
static final String SCHEMA = "JPA_MAPPING_ORPHANREMOVAL";
private static final Logger LOGGER = Logger.getLogger(OrphanRemovalTest.class.getName());
private static long ownerId, noneId, removeId, orphanId;
@EJB
Repository repo;
@Rule
public TestName name = new TestName();
/*
* ------------
* | LIFE CYCLE |
* ------------
*/
@Before
public void persistTestees() {
LOGGER.info(() -> "RUNNING: " + name.getMethodName());
persistNewTestees();
}
private void persistNewTestees() {
repo.applyWithEM(em -> {
Owner owner = new Owner();
em.persist(owner);
CascadeNone none = new CascadeNone();
em.persist(none);
owner.addCascadeNone(none);
CascadeRemove remove = new CascadeRemove();
em.persist(remove);
owner.addCascadeRemove(remove);
OrphanRemoval orphan = new OrphanRemoval();
em.persist(orphan);
owner.addOrphanRemoval(orphan);
ownerId = owner.getId();
noneId = none.getId();
removeId = remove.getId();
orphanId = orphan.getId();
LOGGER.info(() ->
"Persisted owner #" + ownerId +
", none #" + noneId +
", remove #" + removeId +
" and orphan #" + orphanId + ".");
});
assertNotNull(repo.find(Owner.class, ownerId));
assertNotNull(repo.find(CascadeNone.class, noneId));
assertNotNull(repo.find(CascadeRemove.class, removeId));
assertNotNull(repo.find(OrphanRemoval.class, orphanId));
}
/*
* -------
* | TESTS |
* -------
*/
/**
* Removing the owner remove the non-owning {@code CascadeRemove} entity
* <strong>and</strong> the non-owning {@code OrphanRemoved} entity.<p>
*
* JavaDoc of {@code @OneToMany} says that {@code orphanRemoval = true} also
* "cascade the remove operation to those entities". Hence it is not
* necessary to add {@code cascade = CascadeType.REMOVE} on a relationship
* that says {@code orphanRemoval = true}.
*/
@Test
public void removingOwnerRemoveCascadeAndOrphanEntities() {
repo.remove(Owner.class, ownerId);
// Left behind:
assertNotNull(repo.find(CascadeNone.class, noneId));
// Removed:
assertNull(repo.find(CascadeRemove.class, removeId));
assertNull(repo.find(OrphanRemoval.class, orphanId));
}
/**
* Can not remove a non-owning entity as long as he is the target of a
* relationship.<p>
*
* Somewhere in the stack, we expect to find a
* {@code java.sql.SQLIntegrityConstraintViolationException}.
*/
@Test
public void cannotRemoveNonOwningEntityInUse_CascadeNone() {
try {
repo.remove(CascadeNone.class, noneId);
fail("Must not be able to remove a non-owning entity in use!");
}
catch (EJBException e) {
/*
GF:
javax.ejb.EJBException: Transaction aborted
Caused by: javax.transaction.RollbackException: Transaction marked for rollback.
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Caused by: java.sql.SQLIntegrityConstraintViolationException: DELETE on table 'CASCADE_NONE' caused a violation of foreign key constraint 'FK_TFEMGL0JYRM7AJ2F5SO9X8L9W' for key (102). The statement has been rolled back.
Caused by: org.apache.derby.client.am.SqlException: DELETE on table 'CASCADE_NONE' caused a violation of foreign key constraint 'FK_TFEMGL0JYRM7AJ2F5SO9X8L9W' for key (102). The statement has been rolled back.
WF:
Note that WildFly throw a specialized
EJBTransactionRolledbackException. See:
com.martinandersson.javaee.ejb.exceptions.EJBTransactionRolledbackExceptionTest
javax.ejb.EJBTransactionRolledbackException: Transaction rolled back
Caused by: javax.transaction.RollbackException: ARJUNA016053: Could not commit transaction.
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: java.sql.SQLIntegrityConstraintViolationException: DELETE on table 'CASCADE_NONE' caused a violation of foreign key constraint 'FK_TFEMGL0JYRM7AJ2F5SO9X8L9W' for key (76). The statement has been rolled back.
Caused by: org.apache.derby.client.am.SqlException: DELETE on table 'CASCADE_NONE' caused a violation of foreign key constraint 'FK_TFEMGL0JYRM7AJ2F5SO9X8L9W' for key (76). The statement has been rolled back.
*/
assertTrue(causedBy(e, SQLIntegrityConstraintViolationException.class));
}
}
/**
* Can not remove a non-owning entity as long as he is the target of a
* relationship.<p>
*
* Somewhere in the stack, we expect to find a
* {@code java.sql.SQLIntegrityConstraintViolationException}.
*/
@Test
public void cannotRemoveNonOwningEntityInUse_CascadeRemove() {
try {
repo.remove(CascadeRemove.class, removeId);
fail("Must not be able to remove a non-owning entity in use!");
}
catch (EJBException e) {
assertTrue(causedBy(e, SQLIntegrityConstraintViolationException.class));
}
}
/**
* Can not remove a non-owning entity as long as he is the target of a
* relationship.<p>
*
* Somewhere in the stack, we expect to find a
* {@code java.sql.SQLIntegrityConstraintViolationException}.
*/
@Test
public void cannotRemoveNonOwningEntityInUse_OrphanRemove() {
try {
repo.remove(OrphanRemoval.class, orphanId);
fail("Must not be able to remove a non-owning entity in use!");
}
catch (EJBException e) {
assertTrue(causedBy(e, SQLIntegrityConstraintViolationException.class));
}
}
/**
* Removing a non-owning entity from the relationship of his owner will
* leave the entity untouched in database. Only {@code orphanRemoval = true}
* will actually remove the entity too.
*/
@Test
public void removeFromNoCascadeRelationship_entityLeftBehind() {
repo.applyWithEM(em -> {
Owner owner = em.find(Owner.class, ownerId);
CascadeNone none = em.find(CascadeNone.class, noneId);
owner.removeCascadeNone(none);
});
// Non-owning entity left behind:
assertNotNull(repo.find(CascadeNone.class, noneId));
// But entity is not present in the relationship anymore:
Owner owner = repo.find(Owner.class, ownerId);
assertTrue(owner.getCascadeNones().isEmpty());
// Everyone else is untouched:
assertCascadeRemoveIsNotFound();
assertOrphanRemovalIsNotFound();
}
/**
* Removing a non-owning entity from the relationship of his owner will
* leave the entity untouched in database. Only {@code orphanRemoval = true}
* will actually remove the entity too.
*/
@Test
public void removeFromCascadeRelationship_entityLeftBehind() {
repo.applyWithEM(em -> {
Owner owner = em.find(Owner.class, ownerId);
CascadeRemove remove = em.find(CascadeRemove.class, removeId);
owner.removeCascadeRemove(remove);
});
// "CascadeType.REMOVE" does not remove the non-owning entity from db:
assertNotNull(repo.find(CascadeRemove.class, removeId));
// But entity is removed from relationship:
Owner owner = repo.find(Owner.class, ownerId);
assertTrue(owner.getCascadeRemoves().isEmpty());
// Everyone else is untouched:
assertCascadeNoneIsNotFound();
assertOrphanRemovalIsNotFound();
}
/**
* Removing a non-owning entity from a relationship that is marked
* {@code orphanRemoval = true} will remove the entity from database too.<p>
*
* GlassFish pass this test. WildFly/Hibernate don't. WildFly pass
* <strong>only if</strong> the relationship of {@code Owner#orphans} is
* also marked {@code CascadeType.ALL} or {@code CascadeType.PERSIST} (!).
* Setting cascade to {@code CascadeType.REMOVE} does not help.<p>
*
* Related Hibernate bugs:
* <pre>{@code
*
* https://hibernate.atlassian.net/browse/HHH-6709
* https://hibernate.atlassian.net/browse/HHH-6037
* }</pre>
*
* Also see:
* <pre>{@code
*
* http://stackoverflow.com/q/24579374/1268003
* }</pre>
*/
@Test
public void removeFromRelationshipWithOrphanRemoval_entityAlsoRemoved() {
repo.applyWithEM(em -> {
Owner owner = em.find(Owner.class, ownerId);
OrphanRemoval orphan = em.find(OrphanRemoval.class, orphanId);
owner.removeOrphanRemoval(orphan);
});
// Orphan completely evaporated (for GlassFish at least, WildFly crash):
assertNull(repo.find(OrphanRemoval.class, orphanId));
Owner owner = repo.find(Owner.class, ownerId);
assertTrue(owner.getOrphanRemovals().isEmpty());
// Everyone else is untouched:
assertCascadeNoneIsNotFound();
assertCascadeRemoveIsNotFound();
}
/*
* --------------
* | INTERNAL API |
* --------------
*/
private boolean causedBy(Throwable searchIn, Class<? extends Throwable> lookingFor) {
final Throwable cause = searchIn.getCause();
return cause == null ? false :
cause.getClass() == lookingFor || causedBy(cause, lookingFor);
}
private void assertCascadeNoneIsNotFound() {
Owner owner = repo.find(Owner.class, ownerId);
assertTrue(owner.getCascadeNones().size() == 1);
assertTrue(owner.getCascadeNones().contains(repo.find(CascadeNone.class, noneId)));
}
private void assertCascadeRemoveIsNotFound() {
Owner owner = repo.find(Owner.class, ownerId);
assertTrue(owner.getCascadeRemoves().size() == 1);
assertTrue(owner.getCascadeRemoves().contains(repo.find(CascadeRemove.class, removeId)));
}
private void assertOrphanRemovalIsNotFound() {
Owner owner = repo.find(Owner.class, ownerId);
assertTrue(owner.getOrphanRemovals().size() == 1);
assertTrue(owner.getOrphanRemovals().contains(repo.find(OrphanRemoval.class, orphanId)));
}
}