From bcfef8a4e17121110535916d914589d010da03b2 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 21 Jan 2010 12:04:26 +0000 Subject: [PATCH] support for JPA 2.0 TypedQuery interface and query vendor interfaces (SPR-6733) --- .../orm/jpa/SharedEntityManagerCreator.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java index 066e85e4e0..1927749478 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java @@ -28,6 +28,7 @@ import javax.persistence.Query; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; /** @@ -130,9 +131,17 @@ public abstract class SharedEntityManagerCreator { private final Map properties; + private final ClassLoader proxyClassLoader; + public SharedEntityManagerInvocationHandler(EntityManagerFactory target, Map properties) { this.targetFactory = target; this.properties = properties; + if (this.targetFactory instanceof EntityManagerFactoryInfo) { + this.proxyClassLoader = ((EntityManagerFactoryInfo) this.targetFactory).getBeanClassLoader(); + } + else { + this.proxyClassLoader = EntityManagerFactory.class.getClassLoader(); + } } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { @@ -225,7 +234,8 @@ public abstract class SharedEntityManagerCreator { if (result instanceof Query) { Query query = (Query) result; if (isNewEm) { - result = Proxy.newProxyInstance(Query.class.getClassLoader(), new Class[] {Query.class}, + Class[] ifcs = ClassUtils.getAllInterfacesForClass(query.getClass(), this.proxyClassLoader); + result = Proxy.newProxyInstance(this.proxyClassLoader, ifcs, new DeferredQueryInvocationHandler(query, target)); isNewEm = false; } @@ -257,7 +267,7 @@ public abstract class SharedEntityManagerCreator { private final EntityManager em; - private DeferredQueryInvocationHandler(Query target, EntityManager em) { + public DeferredQueryInvocationHandler(Query target, EntityManager em) { this.target = target; this.em = em; }