|
28 | 28 | import org.apache.commons.logging.Log; |
29 | 29 | import org.apache.commons.logging.LogFactory; |
30 | 30 |
|
| 31 | +import org.springframework.util.ClassUtils; |
31 | 32 | import org.springframework.util.CollectionUtils; |
32 | 33 |
|
33 | 34 | /** |
@@ -130,9 +131,17 @@ private static class SharedEntityManagerInvocationHandler implements InvocationH |
130 | 131 |
|
131 | 132 | private final Map properties; |
132 | 133 |
|
| 134 | + private final ClassLoader proxyClassLoader; |
| 135 | + |
133 | 136 | public SharedEntityManagerInvocationHandler(EntityManagerFactory target, Map properties) { |
134 | 137 | this.targetFactory = target; |
135 | 138 | this.properties = properties; |
| 139 | + if (this.targetFactory instanceof EntityManagerFactoryInfo) { |
| 140 | + this.proxyClassLoader = ((EntityManagerFactoryInfo) this.targetFactory).getBeanClassLoader(); |
| 141 | + } |
| 142 | + else { |
| 143 | + this.proxyClassLoader = EntityManagerFactory.class.getClassLoader(); |
| 144 | + } |
136 | 145 | } |
137 | 146 |
|
138 | 147 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
@@ -225,7 +234,8 @@ else if (method.getName().equals("unwrap")) { |
225 | 234 | if (result instanceof Query) { |
226 | 235 | Query query = (Query) result; |
227 | 236 | if (isNewEm) { |
228 | | - result = Proxy.newProxyInstance(Query.class.getClassLoader(), new Class[] {Query.class}, |
| 237 | + Class[] ifcs = ClassUtils.getAllInterfacesForClass(query.getClass(), this.proxyClassLoader); |
| 238 | + result = Proxy.newProxyInstance(this.proxyClassLoader, ifcs, |
229 | 239 | new DeferredQueryInvocationHandler(query, target)); |
230 | 240 | isNewEm = false; |
231 | 241 | } |
@@ -257,7 +267,7 @@ private static class DeferredQueryInvocationHandler implements InvocationHandler |
257 | 267 |
|
258 | 268 | private final EntityManager em; |
259 | 269 |
|
260 | | - private DeferredQueryInvocationHandler(Query target, EntityManager em) { |
| 270 | + public DeferredQueryInvocationHandler(Query target, EntityManager em) { |
261 | 271 | this.target = target; |
262 | 272 | this.em = em; |
263 | 273 | } |
|
0 commit comments