Skip to content

Ignore offset for Limit based query #3454

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 @@ -30,6 +30,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Jens Schauder
* @author Yanming Zhou
*/
public class ParameterBinder {

Expand Down Expand Up @@ -101,7 +102,11 @@ Query bindAndPrepare(Query query, QueryParameterSetter.QueryMetadata metadata,
return query;
}

query.setFirstResult(PageableUtils.getOffsetAsInteger(accessor.getPageable()));
// see #3242
if (!parameters.hasLimitParameter()) {
// offset is meaningless if Limit parameter present
query.setFirstResult(PageableUtils.getOffsetAsInteger(accessor.getPageable()));
}
query.setMaxResults(accessor.getPageable().getPageSize());

return query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Temporal;
Expand All @@ -55,6 +56,7 @@
* @author Thomas Darimont
* @author Jens Schauder
* @author Mark Paluch
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -88,6 +90,8 @@ interface SampleRepository extends Repository<User, Long> {

User validWithPageable(@Param("username") String username, Pageable pageable);

User validWithLimit(@Param("username") String username, Limit limit);

User validWithSort(@Param("username") String username, Sort sort);

User validWithDefaultTemporalTypeParameter(@Temporal Date registerDate);
Expand Down Expand Up @@ -124,6 +128,40 @@ void bindWorksWithNullForPageable() throws Exception {
verify(query).setParameter(eq(1), eq("foo"));
}

@Test
void bindAndPrepareWorksWithPageable() throws Exception {

Method validWithPageable = SampleRepository.class.getMethod("validWithPageable", String.class, Pageable.class);

Object[] values = { "foo", Pageable.ofSize(10).withPage(3) };
bindAndPrepare(validWithPageable, values);
verify(query).setParameter(eq(1), eq("foo"));
verify(query).setFirstResult(eq(30));
verify(query).setMaxResults(eq(10));
}

@Test
void bindWorksWithNullForLimit() throws Exception {

Method validWithLimit = SampleRepository.class.getMethod("validWithLimit", String.class, Limit.class);

Object[] values = { "foo", null };
bind(validWithLimit, values);
verify(query).setParameter(eq(1), eq("foo"));
}

@Test
void bindAndPrepareWorksWithLimit() throws Exception {

Method validWithLimit = SampleRepository.class.getMethod("validWithLimit", String.class, Limit.class);

Object[] values = { "foo", Limit.of(10) };
bindAndPrepare(validWithLimit, values);
verify(query).setParameter(eq(1), eq("foo"));
verify(query).setMaxResults(eq(10));
verify(query, never()).setFirstResult(anyInt());
}

@Test
void usesIndexedParametersIfNoParamAnnotationPresent() {

Expand Down Expand Up @@ -238,6 +276,11 @@ private void bind(Method method, JpaParameters parameters, Object[] values) {
getAccessor(method, values), QueryParameterSetter.ErrorHandling.STRICT);
}

private void bindAndPrepare(Method method, Object[] values) {
ParameterBinderFactory.createBinder(createParameters(method)).bindAndPrepare(query,
new QueryParameterSetter.QueryMetadata(query), getAccessor(method, values));
}

private JpaParametersParameterAccessor getAccessor(Method method, Object... values) {
return new JpaParametersParameterAccessor(createParameters(method), values);
}
Expand Down
Loading