Skip to content

Commit a47bd23

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 6280bd2 commit a47bd23

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,26 @@ public void shouldApplyPageableCorrectlyWhenUsingFindAll() {
203203
repo.save(Arrays.asList(eddard, robb, jon));
204204

205205
Page<Person> firstPage = repo.findAll(new PageRequest(0, 2));
206-
assertThat(firstPage.getContent(), hasSize(2));
207-
assertThat(repo.findAll(firstPage.nextPageable()).getContent(), hasSize(1));
206+
assertThat(firstPage.getContent(), hasSize(2));
207+
assertThat(repo.findAll(firstPage.nextPageable()).getContent(), hasSize(1));
208+
}
209+
210+
/**
211+
* @see DATAREDIS-551
212+
*/
213+
@Test
214+
public void shouldApplyPageableCorrectlyWhenUsingFindByWithoutCriteria() {
215+
216+
Person eddard = new Person("eddard", "stark");
217+
Person robb = new Person("robb", "stark");
218+
Person jon = new Person("jon", "snow");
219+
220+
repo.save(Arrays.asList(eddard, robb, jon));
221+
222+
Page<Person> firstPage = repo.findBy(new PageRequest(0, 2));
223+
assertThat(firstPage.getContent(), hasSize(2));
224+
assertThat(firstPage.getTotalElements(), is(equalTo(3L)));
225+
assertThat(repo.findBy(firstPage.nextPageable()).getContent(), hasSize(1));
208226
}
209227

210228
/**
@@ -280,6 +298,8 @@ public static interface PersonRepository extends PagingAndSortingRepository<Pers
280298
List<Person> findByFirstnameOrLastname(String firstname, String lastname);
281299

282300
List<Person> findBy();
301+
302+
Page<Person> findBy(Pageable page);
283303
}
284304

285305
/**

0 commit comments

Comments
 (0)