Skip to content

Commit fa41595

Browse files
quaffschauder
authored andcommitted
Limit single finders max results to 2 for performance.
Closes spring-projects#2594 Original pull request spring-projects#2604
1 parent 54047ed commit fa41595

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public Optional<T> findOne(Predicate predicate) {
9191
Assert.notNull(predicate, "Predicate must not be null");
9292

9393
try {
94-
return Optional.ofNullable(createQuery(predicate).select(path).fetchOne());
94+
return Optional.ofNullable(createQuery(predicate).select(path).limit(2).fetchOne());
9595
} catch (NonUniqueResultException ex) {
9696
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
9797
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public QuerydslJpaRepository(JpaEntityInformation<T, ID> entityInformation, Enti
101101
public Optional<T> findOne(Predicate predicate) {
102102

103103
try {
104-
return Optional.ofNullable(createQuery(predicate).select(path).fetchOne());
104+
return Optional.ofNullable(createQuery(predicate).select(path).limit(2).fetchOne());
105105
} catch (NonUniqueResultException ex) {
106106
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
107107
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public Page<T> findAll(Pageable pageable) {
452452
public Optional<T> findOne(@Nullable Specification<T> spec) {
453453

454454
try {
455-
return Optional.of(getQuery(spec, Sort.unsorted()).getSingleResult());
455+
return Optional.of(getQuery(spec, Sort.unsorted()).setMaxResults(2).getSingleResult());
456456
} catch (NoResultException e) {
457457
return Optional.empty();
458458
}
@@ -482,7 +482,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
482482
try {
483483
return Optional
484484
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
485-
.getSingleResult());
485+
.setMaxResults(2).getSingleResult());
486486
} catch (NoResultException e) {
487487
return Optional.empty();
488488
}

0 commit comments

Comments
 (0)