From 24ca298de2a6a2657865d5147a4931bd55f8c460 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 2 Jun 2016 12:55:15 +0200 Subject: [PATCH] #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. --- .../jpa/projections/CustomerProjection.java | 4 +--- .../jpa/projections/CustomerRepository.java | 12 ++++++++++- .../CustomerRepositoryIntegrationTest.java | 21 +++++++++++++++---- jpa/pom.xml | 4 ++++ .../projections/CustomerRepository.java | 10 +++++++++ .../CustomerRepositoryIntegrationTest.java | 17 +++++++++++++-- 6 files changed, 58 insertions(+), 10 deletions(-) diff --git a/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerProjection.java b/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerProjection.java index 567e6867..fd78146b 100644 --- a/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerProjection.java +++ b/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerProjection.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -16,10 +16,8 @@ package example.springdata.jpa.projections; /** - * * @author Oliver Gierke */ public interface CustomerProjection { - String getFirstname(); } diff --git a/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java b/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java index 3890c654..3ee7ffa3 100644 --- a/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java +++ b/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -18,6 +18,8 @@ package example.springdata.jpa.projections; import java.util.Collection; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; @@ -85,4 +87,12 @@ public interface CustomerRepository extends CrudRepository { * @return */ T findProjectedById(Long id, Class projection); + + /** + * Projections used with pagination. + * + * @param pageable + * @return + */ + Page findPagedProjectedBy(Pageable pageable); } diff --git a/jpa/example/src/test/java/example/springdata/jpa/projections/CustomerRepositoryIntegrationTest.java b/jpa/example/src/test/java/example/springdata/jpa/projections/CustomerRepositoryIntegrationTest.java index 7d7157ca..44a64723 100644 --- a/jpa/example/src/test/java/example/springdata/jpa/projections/CustomerRepositoryIntegrationTest.java +++ b/jpa/example/src/test/java/example/springdata/jpa/projections/CustomerRepositoryIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -26,19 +26,23 @@ 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.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.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; /** - * Integaration tests for {@link CustomerRepository} to show projection capabilities. + * Integration tests for {@link CustomerRepository} to show projection capabilities. * * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration +@SpringBootTest @Transactional public class CustomerRepositoryIntegrationTest { @@ -112,4 +116,13 @@ public class CustomerRepositoryIntegrationTest { assertThat(projectedDave.getFirstname(), is("Dave")); assertThat(((TargetAware) projectedDave).getTarget(), is(instanceOf(Map.class))); } + + @Test + public void supportsProjectionInCombinationWithPagination() { + + Page page = customers + .findPagedProjectedBy(new PageRequest(0, 1, new Sort(Direction.ASC, "lastname"))); + + assertThat(page.getContent().get(0).getFirstname(), is("Carter")); + } } diff --git a/jpa/pom.xml b/jpa/pom.xml index 2451c0fa..853b8f68 100644 --- a/jpa/pom.xml +++ b/jpa/pom.xml @@ -28,6 +28,10 @@ query-by-example + + Ingalls-BUILD-SNAPSHOT + + diff --git a/mongodb/example/src/main/java/example/springdata/mongodb/projections/CustomerRepository.java b/mongodb/example/src/main/java/example/springdata/mongodb/projections/CustomerRepository.java index 8e55256e..b2423255 100644 --- a/mongodb/example/src/main/java/example/springdata/mongodb/projections/CustomerRepository.java +++ b/mongodb/example/src/main/java/example/springdata/mongodb/projections/CustomerRepository.java @@ -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 { * @return */ T findProjectedById(ObjectId id, Class projection); + + /** + * Projections used with pagination. + * + * @param pageable + * @return + */ + Page findPagedProjectedBy(Pageable pageable); } diff --git a/mongodb/example/src/test/java/example/springdata/mongodb/projections/CustomerRepositoryIntegrationTest.java b/mongodb/example/src/test/java/example/springdata/mongodb/projections/CustomerRepositoryIntegrationTest.java index 66669ab8..439668fe 100644 --- a/mongodb/example/src/test/java/example/springdata/mongodb/projections/CustomerRepositoryIntegrationTest.java +++ b/mongodb/example/src/test/java/example/springdata/mongodb/projections/CustomerRepositoryIntegrationTest.java @@ -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 page = customers + .findPagedProjectedBy(new PageRequest(0, 1, new Sort(Direction.ASC, "lastname"))); + + assertThat(page.getContent().get(0).getFirstname(), is("Carter")); + } }