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) {