Limit single finders max results to 2 for performance.

Closes #2594
Original pull request #2604
This commit is contained in:
Yanming Zhou
2022-07-26 09:38:12 +08:00
committed by Jens Schauder
parent 54047edffb
commit fa41595818
3 changed files with 4 additions and 4 deletions

View File

@@ -91,7 +91,7 @@ public class QuerydslJpaPredicateExecutor<T> implements QuerydslPredicateExecuto
Assert.notNull(predicate, "Predicate must not be null");
try {
return Optional.ofNullable(createQuery(predicate).select(path).fetchOne());
return Optional.ofNullable(createQuery(predicate).select(path).limit(2).fetchOne());
} catch (NonUniqueResultException ex) {
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
}

View File

@@ -101,7 +101,7 @@ public class QuerydslJpaRepository<T, ID extends Serializable> extends SimpleJpa
public Optional<T> findOne(Predicate predicate) {
try {
return Optional.ofNullable(createQuery(predicate).select(path).fetchOne());
return Optional.ofNullable(createQuery(predicate).select(path).limit(2).fetchOne());
} catch (NonUniqueResultException ex) {
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
}

View File

@@ -452,7 +452,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
public Optional<T> findOne(@Nullable Specification<T> spec) {
try {
return Optional.of(getQuery(spec, Sort.unsorted()).getSingleResult());
return Optional.of(getQuery(spec, Sort.unsorted()).setMaxResults(2).getSingleResult());
} catch (NoResultException e) {
return Optional.empty();
}
@@ -482,7 +482,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
try {
return Optional
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
.getSingleResult());
.setMaxResults(2).getSingleResult());
} catch (NoResultException e) {
return Optional.empty();
}