DATAMONGO-446 - Fixed bug in paging query methods returning Lists

Using List as return type for paginating methods didn't work for query methods currently. Fixed by inspecting the Pageable parameter potentially handed into them and restricting the result set accordingly.
This commit is contained in:
Maciej Walkowiak
2012-05-09 16:59:42 +02:00
committed by Oliver Gierke
parent fd198c172b
commit 7cdf9cedf3
3 changed files with 26 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2010-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -85,7 +85,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
} else if (method.isGeoNearQuery()) {
return new GeoNearExecution(accessor).execute(query);
} else if (method.isCollectionQuery()) {
return new CollectionExecution().execute(query);
return new CollectionExecution(accessor.getPageable()).execute(query);
} else if (method.isPageQuery()) {
return new PagedExecution(accessor.getPageable()).execute(query);
} else {
@@ -133,12 +133,23 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
*/
class CollectionExecution extends Execution {
private final Pageable pageable;
CollectionExecution(Pageable pageable) {
this.pageable = pageable;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery.Execution#execute(org.springframework.data.mongodb.core.query.Query)
*/
@Override
public Object execute(Query query) {
if (pageable != null) {
query = applyPagination(query, pageable);
}
return readCollection(query);
}
}

View File

@@ -318,6 +318,16 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
assertThat(females.get(0), is(alicia));
}
/**
* @see DATAMONGO-446
*/
@Test
public void findsPeopleBySexPaginated() {
List<Person> males = repository.findBySex(Sex.MALE, new PageRequest(0, 2));
assertThat(males.size(), is(2));
}
@Test
public void findsPeopleByNamedQuery() {
List<Person> result = repository.findByNamedQuery("Dave");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2010-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -147,6 +147,8 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
List<Person> findBySex(Sex sex);
List<Person> findBySex(Sex sex, Pageable pageable);
List<Person> findByNamedQuery(String firstname);
GeoResults<Person> findByLocationNear(Point point, Distance maxDistance);