bjornharvold
Aug 11th, 2011, 04:38 PM
Hi,
I am using spring-data-document M3 and I am having no success trying to work with paginated results.
I've tried 2 different ways:
My test:
index = 0
maxResults = 1
My Mongo Repository interface method
@Query("{ 'ctrlnm' : ?0 }")
Page<Item> findByCategoryUrlName(String categoryUrlName, Pageable pageable);
When I call this method I get all results back.
Any ideas?
Cheers
bjorn
Update: A picture is worth a 1000 words. Here I have a total result set of 637. In my test I ask for page 1 and size 10. As you can see I get back all 637 items and the numberOfElements method returns 637. Page.toString() returns 1 out of 64 pages, which is correct...had I only retrieved 10 items.
http://www.screencast.com/t/yZImH3voN
The tests below pass. Should I assume there is something wrong with Pageable and Mongo repo interfaces?
@Test
public void testPagination() {
for (int i = 0; i < 50; i++) {
Foo f = new Foo();
f.setTwo(RandomStringUtils.randomAlphabetic(10));
fooRepository.save(f);
}
for (int i = 0; i < 5; i++) {
Page<Foo> page = fooRepository.findAll(new PageRequest(i, 10));
assertNotNull("page is null", page);
assertEquals("page number is incorrect", i, page.getNumber(), 0);
assertEquals("page number of elements is incorrect", 10, page.getNumberOfElements(), 0);
assertEquals("page total number of elements is incorrect", 50l, page.getTotalElements(), 0);
assertEquals("page total pages is incorrect", 5, page.getTotalPages(), 0);
assertEquals("page size is incorrect", 10, page.getSize(), 0);
assertEquals("page list size is incorrect", 10, page.getContent().size(), 0);
assertTrue("page has content is incorrect", page.hasContent());
if (i == 0) {
assertTrue("has next is incorrect", page.hasNextPage());
assertFalse("has previous is incorrect", page.hasPreviousPage());
} else if (i > 0 && i < 4) {
assertTrue("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
} else if (i == 4) {
assertFalse("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
}
}
for (int i = 0; i < 5; i++) {
Page<Foo> page = fooRepository.findAll(QFoo.foo.two.ne("a"), new PageRequest(i, 10));
assertNotNull("page is null", page);
assertEquals("page number is incorrect", i, page.getNumber(), 0);
assertEquals("page number of elements is incorrect", 10, page.getNumberOfElements(), 0);
assertEquals("page total number of elements is incorrect", 50l, page.getTotalElements(), 0);
assertEquals("page total pages is incorrect", 5, page.getTotalPages(), 0);
assertEquals("page size is incorrect", 10, page.getSize(), 0);
assertEquals("page list size is incorrect", 10, page.getContent().size(), 0);
assertTrue("page has content is incorrect", page.hasContent());
if (i == 0) {
assertTrue("has next is incorrect", page.hasNextPage());
assertFalse("has previous is incorrect", page.hasPreviousPage());
} else if (i > 0 && i < 4) {
assertTrue("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
} else if (i == 4) {
assertFalse("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
}
}
}
I am using spring-data-document M3 and I am having no success trying to work with paginated results.
I've tried 2 different ways:
My test:
index = 0
maxResults = 1
My Mongo Repository interface method
@Query("{ 'ctrlnm' : ?0 }")
Page<Item> findByCategoryUrlName(String categoryUrlName, Pageable pageable);
When I call this method I get all results back.
Any ideas?
Cheers
bjorn
Update: A picture is worth a 1000 words. Here I have a total result set of 637. In my test I ask for page 1 and size 10. As you can see I get back all 637 items and the numberOfElements method returns 637. Page.toString() returns 1 out of 64 pages, which is correct...had I only retrieved 10 items.
http://www.screencast.com/t/yZImH3voN
The tests below pass. Should I assume there is something wrong with Pageable and Mongo repo interfaces?
@Test
public void testPagination() {
for (int i = 0; i < 50; i++) {
Foo f = new Foo();
f.setTwo(RandomStringUtils.randomAlphabetic(10));
fooRepository.save(f);
}
for (int i = 0; i < 5; i++) {
Page<Foo> page = fooRepository.findAll(new PageRequest(i, 10));
assertNotNull("page is null", page);
assertEquals("page number is incorrect", i, page.getNumber(), 0);
assertEquals("page number of elements is incorrect", 10, page.getNumberOfElements(), 0);
assertEquals("page total number of elements is incorrect", 50l, page.getTotalElements(), 0);
assertEquals("page total pages is incorrect", 5, page.getTotalPages(), 0);
assertEquals("page size is incorrect", 10, page.getSize(), 0);
assertEquals("page list size is incorrect", 10, page.getContent().size(), 0);
assertTrue("page has content is incorrect", page.hasContent());
if (i == 0) {
assertTrue("has next is incorrect", page.hasNextPage());
assertFalse("has previous is incorrect", page.hasPreviousPage());
} else if (i > 0 && i < 4) {
assertTrue("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
} else if (i == 4) {
assertFalse("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
}
}
for (int i = 0; i < 5; i++) {
Page<Foo> page = fooRepository.findAll(QFoo.foo.two.ne("a"), new PageRequest(i, 10));
assertNotNull("page is null", page);
assertEquals("page number is incorrect", i, page.getNumber(), 0);
assertEquals("page number of elements is incorrect", 10, page.getNumberOfElements(), 0);
assertEquals("page total number of elements is incorrect", 50l, page.getTotalElements(), 0);
assertEquals("page total pages is incorrect", 5, page.getTotalPages(), 0);
assertEquals("page size is incorrect", 10, page.getSize(), 0);
assertEquals("page list size is incorrect", 10, page.getContent().size(), 0);
assertTrue("page has content is incorrect", page.hasContent());
if (i == 0) {
assertTrue("has next is incorrect", page.hasNextPage());
assertFalse("has previous is incorrect", page.hasPreviousPage());
} else if (i > 0 && i < 4) {
assertTrue("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
} else if (i == 4) {
assertFalse("has next is incorrect", page.hasNextPage());
assertTrue("has previous is incorrect", page.hasPreviousPage());
}
}
}