Skip to content

Commit f128375

Browse files
quaffgregturn
authored andcommitted
Limit single finders max results to 2 for performance.
Closes #2594 Original pull request #2604
1 parent 2d4a166 commit f128375

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public Optional<T> findOne(Predicate predicate) {
9595
Assert.notNull(predicate, "Predicate must not be null!");
9696

9797
try {
98-
return Optional.ofNullable(createQuery(predicate).select(path).fetchOne());
98+
return Optional.ofNullable(createQuery(predicate).select(path).limit(2).fetchOne());
9999
} catch (NonUniqueResultException ex) {
100100
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
101101
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public QuerydslJpaRepository(JpaEntityInformation<T, ID> entityInformation, Enti
105105
public Optional<T> findOne(Predicate predicate) {
106106

107107
try {
108-
return Optional.ofNullable(createQuery(predicate).select(path).fetchOne());
108+
return Optional.ofNullable(createQuery(predicate).select(path).limit(2).fetchOne());
109109
} catch (NonUniqueResultException ex) {
110110
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
111111
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public Page<T> findAll(Pageable pageable) {
490490
public Optional<T> findOne(@Nullable Specification<T> spec) {
491491

492492
try {
493-
return Optional.of(getQuery(spec, Sort.unsorted()).getSingleResult());
493+
return Optional.of(getQuery(spec, Sort.unsorted()).setMaxResults(2).getSingleResult());
494494
} catch (NoResultException e) {
495495
return Optional.empty();
496496
}
@@ -536,7 +536,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
536536
try {
537537
return Optional
538538
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
539-
.getSingleResult());
539+
.setMaxResults(2).getSingleResult());
540540
} catch (NoResultException e) {
541541
return Optional.empty();
542542
}

0 commit comments

Comments
 (0)