Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2008-2021 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.repository.query;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.jpa.provider.PersistenceProvider;
import org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.data.util.CloseableIterator;
import org.springframework.data.util.StreamUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Set of classes to contain query execution strategies. Depending (mostly) on the return type of a
* {@link org.springframework.data.repository.query.QueryMethod} a {@link AbstractStringBasedJpaQuery} can be executed
* in various flavors.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Christoph Strobl
* @author Nicolas Cirigliano
* @author Jens Schauder
*/
public abstract class JpaQueryExecution {
private static final ConversionService CONVERSION_SERVICE;
static {
ConfigurableConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(JpaResultConverters.BlobToByteArrayConverter.INSTANCE);
conversionService.removeConvertible(Collection.class, Object.class);
potentiallyRemoveOptionalConverter(conversionService);
CONVERSION_SERVICE = conversionService;
}
/**
* Executes the given {@link AbstractStringBasedJpaQuery} with the given {@link ParameterBinder}.
*
* @param query must not be {@literal null}.
* @param values must not be {@literal null}.
* @return
*/
@Nullable
public Object execute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {
Assert.notNull(query, "AbstractJpaQuery must not be null!");
Assert.notNull(accessor, "JpaParametersParameterAccessor must not be null!");
Object result;
try {
result = doExecute(query, accessor);
} catch (NoResultException e) {
return null;
}
if (result == null) {
return null;
}
JpaQueryMethod queryMethod = query.getQueryMethod();
Class requiredType = queryMethod.getReturnType();
if (ClassUtils.isAssignable(requiredType, void.class) || ClassUtils.isAssignableValue(requiredType, result)) {
return result;
}
return CONVERSION_SERVICE.canConvert(result.getClass(), requiredType) //
? CONVERSION_SERVICE.convert(result, requiredType) //
: result;
}
/**
* Method to implement {@link AbstractStringBasedJpaQuery} executions by single enum values.
*
* @param query
* @param values
* @return
*/
@Nullable
protected abstract Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor);
/**
* Executes the query to return a simple collection of entities.
*/
static class CollectionExecution extends JpaQueryExecution {
@Override
protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {
return query.createQuery(accessor).getResultList();
}
}
/**
* Executes the query to return a {@link Slice} of entities.
*
* @author Oliver Gierke
* @since 1.6
*/
static class SlicedExecution extends JpaQueryExecution {
/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.query.JpaQueryExecution#doExecute(org.springframework.data.jpa.repository.query.AbstractJpaQuery, java.lang.Object[])
*/
@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {
Pageable pageable = accessor.getPageable();
Query createQuery = query.createQuery(accessor);
int pageSize = 0;
if (pageable.isPaged()) {
pageSize = pageable.getPageSize();
createQuery.setMaxResults(pageSize + 1);
}
List