Skip to content

Limit single finders max results to 2 for performance #2604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Optional<T> findOne(Predicate predicate) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public QuerydslJpaRepository(JpaEntityInformation<T, ID> entityInformation, Enti
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public Page<T> findAll(Pageable pageable) {
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();
}
Expand Down Expand Up @@ -482,7 +482,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
try {
return Optional
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
.getSingleResult());
.setMaxResults(2).getSingleResult());
} catch (NoResultException e) {
return Optional.empty();
}
Expand Down