DATAJPA-1110 - Adapt to QueryByExampleExecutor API changes.

Use Optional as return type for findOne(Example example).

Related ticket: DATACMNS-1058.

Original pull request: #201.
This commit is contained in:
Christoph Strobl
2017-05-05 15:18:51 +02:00
committed by Mark Paluch
parent eebe9b51cc
commit efac93c8f3
2 changed files with 7 additions and 5 deletions

View File

@@ -398,11 +398,13 @@ public class SimpleJpaRepository<T, ID> implements JpaRepository<T, ID>, JpaSpec
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> S findOne(Example<S> example) {
public <S extends T> Optional<S> findOne(Example<S> example) {
try {
return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), (Sort) null).getSingleResult();
return Optional
.of(getQuery(new ExampleSpecification<S>(example), example.getProbeType(), (Sort) null).getSingleResult());
} catch (NoResultException e) {
return null;
return Optional.empty();
}
}

View File

@@ -43,6 +43,7 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.assertj.core.api.Assertions;
import org.hamcrest.Matchers;
import org.junit.Assume;
import org.junit.Before;
@@ -2093,9 +2094,8 @@ public class UserRepositoryTests {
prototype.setAge(28);
Example<User> example = Example.of(prototype, matching().withIgnorePaths("createdAt"));
User users = repository.findOne(example);
assertThat(users, is(firstUser));
Assertions.assertThat(repository.findOne(example)).contains(firstUser);
}
@Test // DATAJPA-218