DATAJPA-1379 - Polishing.

Fixed and completed JavaDoc.
Utilized type inference for generics.
Fixed inconsistent access modifiers of private interfaces.
Removed unused constants.
Applied IDE code recommendation to Array and Collection construction.

Original pull request: #287.
This commit is contained in:
Jens Schauder
2018-07-31 09:32:16 +02:00
committed by Mark Paluch
parent a0ba2cfc63
commit c46d6e34bd
3 changed files with 53 additions and 54 deletions

View File

@@ -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;

View File

@@ -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 <a href="https://jira.spring.io/browse/DATAJPA-444">DATAJPA-444</a>
*/
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<Object> executeQueryWithResultStream(Query jpaQuery) {
return new EclipseLinkScrollableResultsIterator<Object>(jpaQuery);
return new EclipseLinkScrollableResultsIterator<>(jpaQuery);
}
},
/**
* Unknown special provider. Use standard JPA.
*/
GENERIC_JPA(Collections.singleton(GENERIC_JPA_ENTITY_MANAGER_INTERFACE), Collections.<String> 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<Class<?>, PersistenceProvider> CACHE = new ConcurrentReferenceHashMap<Class<?>, PersistenceProvider>();
private final Iterable<String> entityManagerClassNames;
private final Iterable<String> metamodelClassNames;
static ConcurrentReferenceHashMap<Class<?>, 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<String> entityManagerClassNames, Iterable<String> metamodelClassNames) {
PersistenceProvider(Iterable<String> entityManagerClassNames, Iterable<String> metamodelClassNames) {
this.entityManagerClassNames = entityManagerClassNames;
this.metamodelClassNames = metamodelClassNames;
}
private final Iterable<String> entityManagerClassNames;
private final Iterable<String> 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 <a href="https://jira.spring.io/browse/DATAJPA-606">DATAJPA-606</a>
* @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 <T> Collection<T> potentiallyConvertEmptyCollection(@Nullable Collection<T> 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<Object> 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 <T> the domain type to return≠
* @since 1.8
*/
private static class HibernateScrollableResultsIterator implements CloseableIterator<Object> {
@@ -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);

View File

@@ -152,12 +152,12 @@ public class PersistenceProviderUnitTests {
private static String[] toResourcePaths(Class<?>... interfacesToImplement) {
List<String> interfaceResourcePaths = new ArrayList<String>(interfacesToImplement.length);
List<String> 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]);
}
}
}