From efac93c8f322f08a0262acc6d77ff30e8fe0adbf Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 5 May 2017 15:18:51 +0200 Subject: [PATCH] DATAJPA-1110 - Adapt to QueryByExampleExecutor API changes. Use Optional as return type for findOne(Example example). Related ticket: DATACMNS-1058. Original pull request: #201. --- .../data/jpa/repository/support/SimpleJpaRepository.java | 8 +++++--- .../data/jpa/repository/UserRepositoryTests.java | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 8b73be075..b0c231370 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -398,11 +398,13 @@ public class SimpleJpaRepository implements JpaRepository, JpaSpec * @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example) */ @Override - public S findOne(Example example) { + public Optional findOne(Example example) { + try { - return getQuery(new ExampleSpecification(example), example.getProbeType(), (Sort) null).getSingleResult(); + return Optional + .of(getQuery(new ExampleSpecification(example), example.getProbeType(), (Sort) null).getSingleResult()); } catch (NoResultException e) { - return null; + return Optional.empty(); } } diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index f860519e6..05daea12e 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -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 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