Skip to content

Commit 44a02e5

Browse files
barreirosebersole
authored andcommitted
HHH-8558 - Bytecode enhancer: testsuite reorganization with added lazy loading tests
1 parent c6fa2b1 commit 44a02e5

36 files changed

+1653
-1567
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/PersistentAttributesEnhancer.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
*/
77
package org.hibernate.bytecode.enhance.internal;
88

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+
918
import javassist.CannotCompileException;
1019
import javassist.CtClass;
1120
import javassist.CtField;
@@ -29,15 +38,6 @@
2938
import org.hibernate.internal.CoreLogging;
3039
import org.hibernate.internal.CoreMessageLogger;
3140

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-
4141
/**
4242
* enhancer for persistent attributes of any type of entity
4343
*
@@ -225,7 +225,7 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
225225
}
226226
final String mappedBy = getMappedBy( persistentField, targetEntity );
227227
if ( mappedBy.isEmpty() ) {
228-
log.debugf(
228+
log.warnf(
229229
"Could not find bi-directional association for field [%s#%s]",
230230
managedCtClass.getName(),
231231
persistentField.getName()
@@ -258,17 +258,18 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
258258
}
259259
if ( persistentField.hasAnnotation( OneToMany.class ) ) {
260260
// only remove elements not in the new collection or else we would loose those elements
261+
// don't use iterator to avoid ConcurrentModException
261262
fieldWriter.insertBefore(
262263
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",
264265
persistentField.getName(),
265266
targetEntity.getName(),
266267
mappedBySetterName
267268
)
268269
);
269270
fieldWriter.insertAfter(
270271
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",
272273
targetEntity.getName(),
273274
mappedByGetterName,
274275
mappedBySetterName,
@@ -287,23 +288,23 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
287288
// check .contains($0) to avoid double inserts (but preventing duplicates)
288289
fieldWriter.insertAfter(
289290
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",
291292
mappedByGetterName
292293
)
293294
);
294295
}
295296
if ( persistentField.hasAnnotation( ManyToMany.class ) ) {
296297
fieldWriter.insertBefore(
297298
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",
299300
persistentField.getName(),
300301
targetEntity.getName(),
301302
mappedByGetterName
302303
)
303304
);
304305
fieldWriter.insertAfter(
305306
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",
307308
targetEntity.getName(),
308309
mappedByGetterName
309310
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.bytecode.enhancement;
8+
9+
import org.hibernate.SessionFactory;
10+
import org.hibernate.cfg.Configuration;
11+
import org.hibernate.cfg.Environment;
12+
import org.hibernate.service.ServiceRegistry;
13+
14+
import org.hibernate.testing.ServiceRegistryBuilder;
15+
16+
/**
17+
* @author Luis Barreiro
18+
*/
19+
public abstract class AbstractEnhancerTestTask implements EnhancerTestTask {
20+
21+
private ServiceRegistry serviceRegistry;
22+
private SessionFactory factory;
23+
24+
public final void prepare(Configuration user) {
25+
Configuration cfg = new Configuration();
26+
cfg.setProperties( user.getProperties() );
27+
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
28+
29+
Class<?>[] resources = getAnnotatedClasses();
30+
for ( Class<?> resource : resources ) {
31+
cfg.addAnnotatedClass( resource );
32+
}
33+
34+
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
35+
factory = cfg.buildSessionFactory( serviceRegistry );
36+
}
37+
38+
public final void complete() {
39+
try {
40+
cleanup();
41+
}
42+
finally {
43+
factory.close();
44+
factory = null;
45+
if ( serviceRegistry != null ) {
46+
ServiceRegistryBuilder.destroy( serviceRegistry );
47+
serviceRegistry = null;
48+
}
49+
}
50+
}
51+
52+
protected SessionFactory getFactory() {
53+
return factory;
54+
}
55+
56+
protected abstract void cleanup();
57+
58+
}

hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/CustomerEnhancerTest.java

Lines changed: 0 additions & 198 deletions
This file was deleted.

0 commit comments

Comments
 (0)