DATACMNS-867 - RepositoryInvoker now returns an Optional for invokeFindOne(…).
Adapted ReflectionRepositoryInvoker to transparently unwrap other potentially used wrapper types to then convert the result into an Optional.
This commit is contained in:
@@ -17,6 +17,7 @@ package org.springframework.data.querydsl;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -125,7 +126,7 @@ public class QuerydslRepositoryInvokerAdapter implements RepositoryInvoker {
|
||||
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeFindOne(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
public <T> T invokeFindOne(Serializable id) {
|
||||
public <T> Optional<T> invokeFindOne(Serializable id) {
|
||||
return delegate.invokeFindOne(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -89,8 +89,8 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker {
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T invokeFindOne(Serializable id) {
|
||||
return customFindOneMethod ? super.invokeFindOne(id) : (T) repository.findOne(convertId(id));
|
||||
public <T> Optional<T> invokeFindOne(Serializable id) {
|
||||
return customFindOneMethod ? super.invokeFindOne(id) : (Optional<T>) repository.findOne(convertId(id));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -152,11 +152,9 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
|
||||
|
||||
Class<?> domainType = targetType.getType();
|
||||
RepositoryInvoker invoker = repositoryInvokerFactory.getInvokerFor(domainType);
|
||||
RepositoryInformation information = repositories.getRequiredRepositoryInformation(domainType);
|
||||
|
||||
return repositories.getRepositoryInformationFor(domainType)//
|
||||
.map(it -> invoker.invokeFindOne(conversionService.convert(source, it.getIdType())))//
|
||||
.orElseThrow(() -> new IllegalStateException(
|
||||
String.format("Couldn't find RepositoryInformation for %s!", domainType)));
|
||||
return invoker.invokeFindOne(conversionService.convert(source, information.getIdType())).orElse(null);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.core.CrudMethods;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.repository.util.QueryExecutionConverters;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -135,12 +136,23 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
public <T> T invokeFindOne(Serializable id) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Optional<T> invokeFindOne(Serializable id) {
|
||||
|
||||
Method method = methods.getFindOneMethod()//
|
||||
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-one-method declared!"));
|
||||
|
||||
return invoke(method, convertId(id));
|
||||
Object invoke = invoke(method, convertId(id));
|
||||
|
||||
if (Optional.class.isInstance(invoke)) {
|
||||
return (Optional<T>) invoke;
|
||||
}
|
||||
|
||||
if (invoke == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return conversionService.convert(QueryExecutionConverters.unwrap(invoke), Optional.class);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -169,7 +181,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
if (idTypes.contains(parameterType)) {
|
||||
invoke(method, convertId(id));
|
||||
} else {
|
||||
invoke(method, this.<Object>invokeFindOne(id));
|
||||
invoke(method, this.<Object> invokeFindOne(id).orElse(null));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -49,7 +50,7 @@ public interface RepositoryInvoker extends RepositoryInvocationInformation {
|
||||
* @return the entity with the given id.
|
||||
* @throws IllegalStateException if the repository does not expose a find-one-method.
|
||||
*/
|
||||
<T> T invokeFindOne(Serializable id);
|
||||
<T> Optional<T> invokeFindOne(Serializable id);
|
||||
|
||||
/**
|
||||
* Invokes the find-all method of the underlying repository using the method taking a {@link Pageable} as parameter if
|
||||
|
||||
Reference in New Issue
Block a user