Skip to content

Commit 5aef8c3

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-551 - Fix pageable query execution when derived criteria is empty.
We now make sure to count records without using criteria when derived criteria is empty. This allows usage of declared query methods using `Pageable` without criteria like `findBy(Pageable page)`. Original Pull Request: #220
1 parent a8462c5 commit 5aef8c3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* Redis specific {@link QueryEngine} implementation.
4141
*
4242
* @author Christoph Strobl
43+
* @author Mark Paluch
4344
* @since 1.7
4445
*/
4546
class RedisQueryEngine extends QueryEngine<RedisKeyValueAdapter, RedisOperationChain, Comparator<?>> {
@@ -156,6 +157,10 @@ public Collection<?> execute(final RedisOperationChain criteria, Comparator<?> s
156157
@Override
157158
public long count(final RedisOperationChain criteria, final Serializable keyspace) {
158159

160+
if(criteria == null) {
161+
return this.getAdapter().count(keyspace);
162+
}
163+
159164
return this.getAdapter().execute(new RedisCallback<Long>() {
160165

161166
@Override

src/test/java/org/springframework/data/redis/repository/RedisRepositoryIntegrationTestBase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ public void shouldApplyPageableCorrectlyWhenUsingFindAll() {
222222
assertThat(repo.findAll(firstPage.nextPageable()).getContent(), hasSize(1));
223223
}
224224

225+
/**
226+
* @see DATAREDIS-551
227+
*/
228+
@Test
229+
public void shouldApplyPageableCorrectlyWhenUsingFindByWithoutCriteria() {
230+
231+
Person eddard = new Person("eddard", "stark");
232+
Person robb = new Person("robb", "stark");
233+
Person jon = new Person("jon", "snow");
234+
235+
repo.save(Arrays.asList(eddard, robb, jon));
236+
237+
Page<Person> firstPage = repo.findBy(new PageRequest(0, 2));
238+
assertThat(firstPage.getContent(), hasSize(2));
239+
assertThat(firstPage.getTotalElements(), is(equalTo(3L)));
240+
assertThat(repo.findBy(firstPage.nextPageable()).getContent(), hasSize(1));
241+
}
242+
225243
/**
226244
* @see DATAREDIS-547
227245
*/
@@ -317,6 +335,8 @@ public static interface PersonRepository extends PagingAndSortingRepository<Pers
317335
List<Person> findTop2By();
318336

319337
List<Person> findTop2ByLastname(String lastname);
338+
339+
Page<Person> findBy(Pageable page);
320340
}
321341

322342
/**

0 commit comments

Comments
 (0)