From 732d6ec261fe834df79ae3bf049aa3d7eadd9b8b Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 13 Aug 2012 18:52:50 +0200 Subject: [PATCH] DATAJPA-244 - Use ClassLoader of EntityManager for implementation lookup. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JpaClasUtils.isEntityManagerOfType(…) now uses the given EntityManager's ClassLoader to try to load the given concrete classname. We're actually using the EntityManager's delegate here which is usually some provider specific implementation (e.g. a Hibernate Session). This assumes that the ClassLoader who initially loaded the class to create the delegate instance will also see the special EntityManager interface. --- .../repository/support/PersistenceProvider.java | 7 +++++-- .../data/jpa/repository/utils/JpaClassUtils.java | 15 ++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/PersistenceProvider.java b/src/main/java/org/springframework/data/jpa/repository/support/PersistenceProvider.java index 84ac5b213..a473dc83a 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/PersistenceProvider.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/PersistenceProvider.java @@ -24,6 +24,7 @@ import org.apache.openjpa.persistence.OpenJPAQuery; import org.eclipse.persistence.jpa.JpaQuery; import org.hibernate.ejb.HibernateQuery; import org.springframework.data.jpa.repository.query.QueryExtractor; +import org.springframework.util.Assert; /** * Enumeration representing peristence providers to be used. @@ -112,11 +113,13 @@ public enum PersistenceProvider implements QueryExtractor { * Determines the {@link PersistenceProvider} from the given {@link EntityManager}. If no special one can be * determined {@value #GENERIC_JPA} will be returned. * - * @param em - * @return + * @param em must not be {@literal null}. + * @return will never be {@literal null}. */ public static PersistenceProvider fromEntityManager(EntityManager em) { + Assert.notNull(em); + for (PersistenceProvider provider : values()) { if (isEntityManagerOfType(em, provider.entityManagerClassName)) { return provider; diff --git a/src/main/java/org/springframework/data/jpa/repository/utils/JpaClassUtils.java b/src/main/java/org/springframework/data/jpa/repository/utils/JpaClassUtils.java index 21793fc12..fd91caf90 100644 --- a/src/main/java/org/springframework/data/jpa/repository/utils/JpaClassUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/utils/JpaClassUtils.java @@ -17,6 +17,9 @@ package org.springframework.data.jpa.repository.utils; import javax.persistence.EntityManager; +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; + /** * Utility class to work with classes. * @@ -34,19 +37,21 @@ public abstract class JpaClassUtils { /** * Returns whether the given {@link EntityManager} is of the given type. * - * @param em - * @param type the fully qualified expected {@link EntityManager} type. + * @param em must not be {@literal null}. + * @param type the fully qualified expected {@link EntityManager} type, must not be {@literal null} or empty. * @return */ - @SuppressWarnings("unchecked") public static boolean isEntityManagerOfType(EntityManager em, String type) { + Assert.notNull(em, "EntityManager must not be null!"); + Assert.hasText(type, "EntityManager type must not be null!"); + try { - Class emType = (Class) Class.forName(type); + ClassLoader loader = em.getDelegate().getClass().getClassLoader(); + Class emType = ClassUtils.forName(type, loader); emType.cast(em); - return true; } catch (Exception e) {