org.springframework.data.jpa.provider.PersistenceProvider Maven / Gradle / Ivy
/*
* Copyright 2008-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.Collection;
import java.util.Collections;
import java.util.NoSuchElementException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.metamodel.Metamodel;
import org.eclipse.persistence.jpa.JpaQuery;
import org.eclipse.persistence.queries.ScrollableCursor;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.proxy.HibernateProxy;
import org.springframework.data.util.CloseableIterator;
import org.springframework.lang.Nullable;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
/**
* Enumeration representing persistence providers to be used.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Jens Schauder
*/
public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor {
/**
* Hibernate persistence provider.
*
* Since Hibernate 4.3 the location of the HibernateEntityManager moved to the org.hibernate.jpa package. In order to
* support both locations we interpret both classnames as a Hibernate {@code PersistenceProvider}.
*
* @see DATAJPA-444
*/
HIBERNATE(//
Collections.singletonList(HIBERNATE_ENTITY_MANAGER_INTERFACE), //
Collections.singletonList(HIBERNATE_JPA_METAMODEL_TYPE)) {
public String extractQueryString(Query query) {
return HibernateUtils.getHibernateQuery(query);
}
/**
* Return custom placeholder ({@code *}) as Hibernate does create invalid queries for count queries for objects with
* compound keys.
*
* @see HHH-4044
* @see HHH-3096
*/
@Override
public String getCountQueryPlaceholder() {
return "*";
}
/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.support.ProxyIdAccessor#isProxy(java.lang.Object)
*/
@Override
public boolean shouldUseAccessorFor(Object entity) {
return entity instanceof HibernateProxy;
}
/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.support.ProxyIdAccessor#getIdentifierFrom(java.lang.Object)
*/
@Override
public Object getIdentifierFrom(Object entity) {
return ((HibernateProxy) entity).getHibernateLazyInitializer().getIdentifier();
}
/*
* (non-Javadoc)
* @see org.springframework.data.jpa.provider.PersistenceProvider#potentiallyConvertEmptyCollection(java.util.Collection)
*/
@Nullable
@Override
public Collection potentiallyConvertEmptyCollection(@Nullable Collection collection) {
return collection == null || collection.isEmpty() ? null : collection;
}
/*
* (non-Javadoc)
* @see org.springframework.data.jpa.provider.PersistenceProvider#executeQueryWithResultStream(javax.persistence.Query)
*/
@Override
public CloseableIterator