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 Greg L. Turnquist
parent 2d4a16655c
commit f128375289
3 changed files with 4 additions and 4 deletions

View File

@@ -95,7 +95,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

@@ -105,7 +105,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

@@ -490,7 +490,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();
}
@@ -536,7 +536,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();
}