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