Skip to content

Commit efac93c

Browse files
christophstroblmp911de
authored andcommitted
DATAJPA-1110 - Adapt to QueryByExampleExecutor API changes.
Use Optional as return type for findOne(Example example). Related ticket: DATACMNS-1058. Original pull request: #201.
1 parent eebe9b5 commit efac93c

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,13 @@ public List<T> findAll(Specification<T> spec, Sort sort) {
398398
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
399399
*/
400400
@Override
401-
public <S extends T> S findOne(Example<S> example) {
401+
public <S extends T> Optional<S> findOne(Example<S> example) {
402+
402403
try {
403-
return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), (Sort) null).getSingleResult();
404+
return Optional
405+
.of(getQuery(new ExampleSpecification<S>(example), example.getProbeType(), (Sort) null).getSingleResult());
404406
} catch (NoResultException e) {
405-
return null;
407+
return Optional.empty();
406408
}
407409
}
408410

src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import javax.persistence.criteria.Predicate;
4444
import javax.persistence.criteria.Root;
4545

46+
import org.assertj.core.api.Assertions;
4647
import org.hamcrest.Matchers;
4748
import org.junit.Assume;
4849
import org.junit.Before;
@@ -2093,9 +2094,8 @@ public void findOneByExampleWithExcludedAttributes() {
20932094
prototype.setAge(28);
20942095

20952096
Example<User> example = Example.of(prototype, matching().withIgnorePaths("createdAt"));
2096-
User users = repository.findOne(example);
20972097

2098-
assertThat(users, is(firstUser));
2098+
Assertions.assertThat(repository.findOne(example)).contains(firstUser);
20992099
}
21002100

21012101
@Test // DATAJPA-218

0 commit comments

Comments
 (0)