diff --git a/src/main/java/org/springframework/data/jpa/provider/JpaClassUtils.java b/src/main/java/org/springframework/data/jpa/provider/JpaClassUtils.java index 44bac78c6..6fe1b1fad 100644 --- a/src/main/java/org/springframework/data/jpa/provider/JpaClassUtils.java +++ b/src/main/java/org/springframework/data/jpa/provider/JpaClassUtils.java @@ -27,6 +27,7 @@ import org.springframework.util.ClassUtils; * * @author Oliver Gierke * @author Christoph Strobl + * @author Jens Schauder */ abstract class JpaClassUtils { @@ -40,9 +41,10 @@ abstract class JpaClassUtils { * * @param em must not be {@literal null}. * @param type the fully qualified expected {@link EntityManager} type, must not be {@literal null} or empty. - * @return + * @return wether the given {@code EntityManager} is of the given type. */ public static boolean isEntityManagerOfType(EntityManager em, String type) { + Object delegate = em.getDelegate(); if (delegate instanceof EntityManager) { em = (EntityManager) delegate; diff --git a/src/main/java/org/springframework/data/jpa/provider/PersistenceProvider.java b/src/main/java/org/springframework/data/jpa/provider/PersistenceProvider.java index b2e9dec20..3b889e63d 100644 --- a/src/main/java/org/springframework/data/jpa/provider/PersistenceProvider.java +++ b/src/main/java/org/springframework/data/jpa/provider/PersistenceProvider.java @@ -18,7 +18,6 @@ package org.springframework.data.jpa.provider; import static org.springframework.data.jpa.provider.JpaClassUtils.*; import static org.springframework.data.jpa.provider.PersistenceProvider.Constants.*; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.NoSuchElementException; @@ -57,8 +56,8 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { * @see DATAJPA-444 */ HIBERNATE(// - Arrays.asList(HIBERNATE_ENTITY_MANAGER_INTERFACE), // - Arrays.asList(HIBERNATE_JPA_METAMODEL_TYPE)) { + Collections.singletonList(HIBERNATE_ENTITY_MANAGER_INTERFACE), // + Collections.singletonList(HIBERNATE_JPA_METAMODEL_TYPE)) { public String extractQueryString(Query query) { return HibernateUtils.getHibernateQuery(query); @@ -117,7 +116,8 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { /** * EclipseLink persistence provider. */ - ECLIPSELINK(Collections.singleton(ECLIPSELINK_ENTITY_MANAGER_INTERFACE), Collections.singleton(ECLIPSELINK_JPA_METAMODEL_TYPE)) { + ECLIPSELINK(Collections.singleton(ECLIPSELINK_ENTITY_MANAGER_INTERFACE), + Collections.singleton(ECLIPSELINK_JPA_METAMODEL_TYPE)) { public String extractQueryString(Query query) { return ((JpaQuery) query).getDatabaseQuery().getJPQLString(); @@ -158,14 +158,14 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { */ @Override public CloseableIterator executeQueryWithResultStream(Query jpaQuery) { - return new EclipseLinkScrollableResultsIterator(jpaQuery); + return new EclipseLinkScrollableResultsIterator<>(jpaQuery); } }, /** * Unknown special provider. Use standard JPA. */ - GENERIC_JPA(Collections.singleton(GENERIC_JPA_ENTITY_MANAGER_INTERFACE), Collections. emptySet()) { + GENERIC_JPA(Collections.singleton(GENERIC_JPA_ENTITY_MANAGER_INTERFACE), Collections.emptySet()) { /* * (non-Javadoc) @@ -206,29 +206,7 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { } }; - /** - * Holds the PersistenceProvider specific interface names. - * - * @author Thomas Darimont - */ - static interface Constants { - - String GENERIC_JPA_ENTITY_MANAGER_INTERFACE = "javax.persistence.EntityManager"; - String OPENJPA_ENTITY_MANAGER_INTERFACE = "org.apache.openjpa.persistence.OpenJPAEntityManager"; - String ECLIPSELINK_ENTITY_MANAGER_INTERFACE = "org.eclipse.persistence.jpa.JpaEntityManager"; - String HIBERNATE_SESSION_INTERFACE = "org.hibernate.Session"; - // needed as Spring only exposes that interface via the EM proxy - String HIBERNATE_ENTITY_MANAGER_INTERFACE = "org.hibernate.jpa.HibernateEntityManager"; - - String HIBERNATE_JPA_METAMODEL_TYPE = "org.hibernate.metamodel.internal.MetamodelImpl"; - String ECLIPSELINK_JPA_METAMODEL_TYPE = "org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl"; - String OPENJPA_JPA_METAMODEL_TYPE = "org.apache.openjpa.persistence.meta.MetamodelImpl"; - } - - static ConcurrentReferenceHashMap, PersistenceProvider> CACHE = new ConcurrentReferenceHashMap, PersistenceProvider>(); - - private final Iterable entityManagerClassNames; - private final Iterable metamodelClassNames; + static ConcurrentReferenceHashMap, PersistenceProvider> CACHE = new ConcurrentReferenceHashMap<>(); /** * Creates a new {@link PersistenceProvider}. @@ -237,12 +215,27 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { * be {@literal null} or empty. * @param metamodelClassNames must not be {@literal null}. */ - private PersistenceProvider(Iterable entityManagerClassNames, Iterable metamodelClassNames) { + PersistenceProvider(Iterable entityManagerClassNames, Iterable metamodelClassNames) { this.entityManagerClassNames = entityManagerClassNames; this.metamodelClassNames = metamodelClassNames; } + private final Iterable entityManagerClassNames; + private final Iterable metamodelClassNames; + + /** + * Caches the given {@link PersistenceProvider} for the given source type. + * + * @param type must not be {@literal null}. + * @param provider must not be {@literal null}. + * @return the {@code PersistenceProvider} passed in as an argument. Guaranteed to be not {@code null}. + */ + private static PersistenceProvider cacheAndReturn(Class type, PersistenceProvider provider) { + CACHE.put(type, provider); + return provider; + } + /** * Determines the {@link PersistenceProvider} from the given {@link EntityManager}. If no special one can be * determined {@link #GENERIC_JPA} will be returned. @@ -302,15 +295,12 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { } /** - * Caches the given {@link PersistenceProvider} for the given source type. + * Returns the placeholder to be used for simple count queries. Default implementation returns {@code x}. * - * @param type must not be {@literal null}. - * @param provider must not be {@literal null}. - * @return + * @return a placeholder for count queries. Guaranteed to be not {@code null}. */ - private static PersistenceProvider cacheAndReturn(Class type, PersistenceProvider provider) { - CACHE.put(type, provider); - return provider; + public String getCountQueryPlaceholder() { + return "x"; } /* @@ -321,28 +311,36 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { return true; } - /** - * Returns the placeholder to be used for simple count queries. Default implementation returns {@code *}. - * - * @return - */ - public String getCountQueryPlaceholder() { - return "x"; - } - /** * Potentially converts an empty collection to the appropriate representation of this {@link PersistenceProvider}, * since some JPA providers cannot correctly handle empty collections. * * @see DATAJPA-606 - * @param collection - * @return + * @param collection The collection to be converted. May be {@code null}. + * @return a potentially converted collection. May be {@code null}. */ @Nullable public Collection potentiallyConvertEmptyCollection(@Nullable Collection collection) { return collection; } + /** + * Holds the PersistenceProvider specific interface names. + * + * @author Thomas Darimont + * @author Jens Schauder + */ + interface Constants { + + String GENERIC_JPA_ENTITY_MANAGER_INTERFACE = "javax.persistence.EntityManager"; + String ECLIPSELINK_ENTITY_MANAGER_INTERFACE = "org.eclipse.persistence.jpa.JpaEntityManager"; + // needed as Spring only exposes that interface via the EM proxy + String HIBERNATE_ENTITY_MANAGER_INTERFACE = "org.hibernate.jpa.HibernateEntityManager"; + + String HIBERNATE_JPA_METAMODEL_TYPE = "org.hibernate.metamodel.internal.MetamodelImpl"; + String ECLIPSELINK_JPA_METAMODEL_TYPE = "org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl"; + } + public CloseableIterator executeQueryWithResultStream(Query jpaQuery) { throw new UnsupportedOperationException( "Streaming results is not implement for this PersistenceProvider: " + name()); @@ -353,7 +351,6 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { * * @author Thomas Darimont * @author Oliver Gierke - * @param the domain type to return≠ * @since 1.8 */ private static class HibernateScrollableResultsIterator implements CloseableIterator { @@ -365,7 +362,7 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { * * @param jpaQuery must not be {@literal null}. */ - public HibernateScrollableResultsIterator(Query jpaQuery) { + HibernateScrollableResultsIterator(Query jpaQuery) { org.hibernate.query.Query query = jpaQuery.unwrap(org.hibernate.query.Query.class); this.scrollableResults = query.setReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly())// @@ -428,7 +425,7 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor { * * @param jpaQuery must not be {@literal null}. */ - public EclipseLinkScrollableResultsIterator(Query jpaQuery) { + EclipseLinkScrollableResultsIterator(Query jpaQuery) { jpaQuery.setHint("eclipselink.cursor.scrollable", true); diff --git a/src/test/java/org/springframework/data/jpa/provider/PersistenceProviderUnitTests.java b/src/test/java/org/springframework/data/jpa/provider/PersistenceProviderUnitTests.java index e63ffc9bd..cae09b547 100644 --- a/src/test/java/org/springframework/data/jpa/provider/PersistenceProviderUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/provider/PersistenceProviderUnitTests.java @@ -152,12 +152,12 @@ public class PersistenceProviderUnitTests { private static String[] toResourcePaths(Class... interfacesToImplement) { - List interfaceResourcePaths = new ArrayList(interfacesToImplement.length); + List interfaceResourcePaths = new ArrayList<>(interfacesToImplement.length); for (Class iface : interfacesToImplement) { interfaceResourcePaths.add(ClassUtils.convertClassNameToResourcePath(iface.getName())); } - return interfaceResourcePaths.toArray(new String[interfaceResourcePaths.size()]); + return interfaceResourcePaths.toArray(new String[0]); } } }