#193 - Added examples for using projections with pagination in JPA and MongoDB.

Requires an upgrade to snapshots as the JPA implementation is broken in Hopper SR1 but will be fixed in SR2 (Boot 1.4 RC1). Switched to use @SpringBootTest in test cases.
This commit is contained in:
Oliver Gierke
2016-06-02 12:55:15 +02:00
parent 12628b6a3f
commit 24ca298de2
6 changed files with 58 additions and 10 deletions

View File

@@ -19,6 +19,8 @@ import java.util.Collection;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
/**
@@ -78,4 +80,12 @@ interface CustomerRepository extends CrudRepository<Customer, ObjectId> {
* @return
*/
<T> T findProjectedById(ObjectId id, Class<T> projection);
/**
* Projections used with pagination.
*
* @param pageable
* @return
*/
Page<CustomerProjection> findPagedProjectedBy(Pageable pageable);
}

View File

@@ -25,8 +25,12 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.projection.TargetAware;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -36,7 +40,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@SpringBootTest
public class CustomerRepositoryIntegrationTest {
@Configuration
@@ -104,4 +108,13 @@ public class CustomerRepositoryIntegrationTest {
assertThat(result.getFirstname(), is("Dave"));
assertThat(((TargetAware) result).getTarget(), is(instanceOf(Customer.class)));
}
@Test
public void supportsProjectionInCombinationWithPagination() {
Page<CustomerProjection> page = customers
.findPagedProjectedBy(new PageRequest(0, 1, new Sort(Direction.ASC, "lastname")));
assertThat(page.getContent().get(0).getFirstname(), is("Carter"));
}
}