#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:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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<Customer, Long> {
|
||||
* @return
|
||||
*/
|
||||
<T> T findProjectedById(Long id, Class<T> projection);
|
||||
|
||||
/**
|
||||
* Projections used with pagination.
|
||||
*
|
||||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
Page<CustomerProjection> findPagedProjectedBy(Pageable pageable);
|
||||
}
|
||||
|
||||
@@ -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<CustomerProjection> page = customers
|
||||
.findPagedProjectedBy(new PageRequest(0, 1, new Sort(Direction.ASC, "lastname")));
|
||||
|
||||
assertThat(page.getContent().get(0).getFirstname(), is("Carter"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,10 @@
|
||||
<module>query-by-example</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<spring-data-releasetrain.version>Ingalls-BUILD-SNAPSHOT</spring-data-releasetrain.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user