Skip to content

Commit 4dc9dc6

Browse files
lgathybrmeyer
authored andcommitted
HHH-3078 Fixed bug with multiple classloaders and proxy class
1 parent df69a3b commit 4dc9dc6

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

hibernate-core/src/main/java/org/hibernate/tuple/entity/PojoEntityTuplizer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,13 @@ public PojoEntityTuplizer(EntityMetamodel entityMetamodel, EntityBinding mappedE
158158
protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) {
159159
// determine the id getter and setter methods from the proxy interface (if any)
160160
// determine all interfaces needed by the resulting proxy
161-
HashSet<Class> proxyInterfaces = new HashSet<Class>();
162-
proxyInterfaces.add( HibernateProxy.class );
161+
/*
162+
* We need to preserve the order of the interfaces they were put into the set, since javassist will choose the
163+
* first one's class-loader to construct the proxy class with. This is also the reason why HibernateProxy.class
164+
* should be the last one in the order (on JBossAS7 its class-loader will be org.hibernate module's class-
165+
* loader, which will not see the classes inside deployed apps.
166+
*/
167+
Set<Class> proxyInterfaces = new java.util.LinkedHashSet<Class>();
163168

164169
Class mappedClass = persistentClass.getMappedClass();
165170
Class proxyInterface = persistentClass.getProxyInterface();
@@ -192,6 +197,8 @@ protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter
192197
}
193198
}
194199

200+
proxyInterfaces.add( HibernateProxy.class );
201+
195202
Iterator properties = persistentClass.getPropertyIterator();
196203
Class clazz = persistentClass.getMappedClass();
197204
while ( properties.hasNext() ) {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.test.proxyclass;
25+
26+
import javax.persistence.Entity;
27+
import javax.persistence.Id;
28+
29+
import org.hibernate.Session;
30+
import org.hibernate.Transaction;
31+
import org.hibernate.annotations.Proxy;
32+
import org.hibernate.proxy.HibernateProxy;
33+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
34+
import org.junit.Assert;
35+
import org.junit.Test;
36+
37+
/**
38+
* Tests if javassist instrumentation is done with the proper classloader for entities with proxy class. The classloader
39+
* of {@link HibernateProxy} will not see {@link IPerson}, since it is only accessible from this package. But: the
40+
* classloader of {@link IPerson} will see {@link HibernateProxy}, so instrumentation will only work if this classloader
41+
* is chosen for creating the instrumented proxy class. We need to check the class of a loaded object though, since
42+
* building the configuration will not fail, only log the error and fall back to using the entity class itself as a
43+
* proxy.
44+
*
45+
* @author lgathy
46+
*/
47+
public class ProxyInterfaceClassLoaderTest extends BaseCoreFunctionalTestCase {
48+
49+
@Override
50+
protected Class<?>[] getAnnotatedClasses() {
51+
return new Class[] { Person.class };
52+
}
53+
54+
@Test
55+
public void testProxyClassLoader() {
56+
57+
Session s = openSession();
58+
Transaction t = s.beginTransaction();
59+
IPerson p = new Person();
60+
p.setId( 1 );
61+
s.persist( p );
62+
s.flush();
63+
s.clear();
64+
65+
Object lp = s.load( Person.class, p.getId() );
66+
67+
Assert.assertTrue( "Loaded entity is not an instance of the proxy interface", IPerson.class.isInstance( lp ) );
68+
Assert.assertFalse( "Proxy class was not created", Person.class.isInstance( lp ) );
69+
70+
s.delete( lp );
71+
t.commit();
72+
s.close();
73+
}
74+
75+
interface IPerson {
76+
77+
int getId();
78+
79+
void setId(int id);
80+
81+
}
82+
83+
@Entity
84+
@Proxy(proxyClass = IPerson.class)
85+
static class Person implements IPerson {
86+
87+
@Id
88+
private int id;
89+
90+
public int getId() {
91+
return id;
92+
}
93+
94+
public void setId(int id) {
95+
this.id = id;
96+
}
97+
98+
}
99+
100+
}

0 commit comments

Comments
 (0)