6
6
*/
7
7
package org .hibernate .bytecode .enhance .internal ;
8
8
9
+ import java .util .IdentityHashMap ;
10
+ import java .util .LinkedList ;
11
+ import java .util .List ;
12
+ import javax .persistence .Embedded ;
13
+ import javax .persistence .ManyToMany ;
14
+ import javax .persistence .ManyToOne ;
15
+ import javax .persistence .OneToMany ;
16
+ import javax .persistence .OneToOne ;
17
+
9
18
import javassist .CannotCompileException ;
10
19
import javassist .CtClass ;
11
20
import javassist .CtField ;
29
38
import org .hibernate .internal .CoreLogging ;
30
39
import org .hibernate .internal .CoreMessageLogger ;
31
40
32
- import javax .persistence .Embedded ;
33
- import javax .persistence .ManyToMany ;
34
- import javax .persistence .ManyToOne ;
35
- import javax .persistence .OneToMany ;
36
- import javax .persistence .OneToOne ;
37
- import java .util .IdentityHashMap ;
38
- import java .util .LinkedList ;
39
- import java .util .List ;
40
-
41
41
/**
42
42
* enhancer for persistent attributes of any type of entity
43
43
*
@@ -225,7 +225,7 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
225
225
}
226
226
final String mappedBy = getMappedBy ( persistentField , targetEntity );
227
227
if ( mappedBy .isEmpty () ) {
228
- log .debugf (
228
+ log .warnf (
229
229
"Could not find bi-directional association for field [%s#%s]" ,
230
230
managedCtClass .getName (),
231
231
persistentField .getName ()
@@ -258,17 +258,18 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
258
258
}
259
259
if ( persistentField .hasAnnotation ( OneToMany .class ) ) {
260
260
// only remove elements not in the new collection or else we would loose those elements
261
+ // don't use iterator to avoid ConcurrentModException
261
262
fieldWriter .insertBefore (
262
263
String .format (
263
- "if ($0.%s != null) for (java.util.Iterator itr = $0.%<s.iterator (); itr.hasNext(); ) { %s target = (%<s) itr.next() ; if ($1 == null || !$1.contains(target)) target.%s(null); }%n" ,
264
+ "if ($0.%s != null) { Object[] array = $0.%<s.toArray (); for (int i = 0; i < array.length; i++ ) { %s target = (%<s) array[i] ; if ($1 == null || !$1.contains(target)) target.%s(null); } }%n" ,
264
265
persistentField .getName (),
265
266
targetEntity .getName (),
266
267
mappedBySetterName
267
268
)
268
269
);
269
270
fieldWriter .insertAfter (
270
271
String .format (
271
- "if ($1 != null) for (java.util.Iterator itr = $1.iterator (); itr.hasNext(); ) { %s target = (%<s) itr.next() ; if (target.%s() != $0) target.%s((%s)$0); }%n" ,
272
+ "if ($1 != null) { Object[] array = $1.toArray (); for (int i = 0; i < array.length; i++ ) { %s target = (%<s) array[i] ; if (target.%s() != $0) target.%s((%s)$0); } }%n" ,
272
273
targetEntity .getName (),
273
274
mappedByGetterName ,
274
275
mappedBySetterName ,
@@ -287,23 +288,23 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
287
288
// check .contains($0) to avoid double inserts (but preventing duplicates)
288
289
fieldWriter .insertAfter (
289
290
String .format (
290
- "if ($1 != null && $1.%s() != null && !$1.%<s(). contains($0) ) $1.%<s(). add($0);%n" ,
291
+ "if ($1 != null) { java.util.Collection c = $1.%s(); if (c != null && !c. contains($0)) c. add($0); } %n" ,
291
292
mappedByGetterName
292
293
)
293
294
);
294
295
}
295
296
if ( persistentField .hasAnnotation ( ManyToMany .class ) ) {
296
297
fieldWriter .insertBefore (
297
298
String .format (
298
- "if ($0.%s != null) for (java.util.Iterator itr = $0.%<s.iterator (); itr.hasNext(); ) { %s target = (%<s) itr.next() ; if ($1 == null || !$1.contains(target)) target.%s().remove($0); }%n" ,
299
+ "if ($0.%s != null) { Object[] array = $0.%<s.toArray (); for (int i = 0; i < array.length; i++ ) { %s target = (%<s) array[i] ; if ($1 == null || !$1.contains(target)) target.%s().remove($0); } }%n" ,
299
300
persistentField .getName (),
300
301
targetEntity .getName (),
301
302
mappedByGetterName
302
303
)
303
304
);
304
305
fieldWriter .insertAfter (
305
306
String .format (
306
- "if ($1 != null) for (java.util.Iterator itr = $1.iterator (); itr.hasNext(); ) { %s target = (%<s) itr.next(); if ( target.%s() != $0 && target.%<s() != null) target.%<s(). add($0); }%n" ,
307
+ "if ($1 != null) { Object[] array = $1.toArray (); for (int i = 0; i < array.length; i++ ) { %s target = (%<s) array[i]; java.util.Collection c = target.%s(); if ( c != $0 && c != null) c. add($0); } }%n" ,
307
308
targetEntity .getName (),
308
309
mappedByGetterName
309
310
)
0 commit comments