#201 - Added example for constructor expression in JPA projections.

This commit is contained in:
Oliver Gierke
2016-07-27 15:08:03 +02:00
parent bee8a81118
commit 15e8ba31cc
2 changed files with 18 additions and 0 deletions

View File

@@ -95,4 +95,13 @@ public interface CustomerRepository extends CrudRepository<Customer, Long> {
* @return
*/
Page<CustomerProjection> findPagedProjectedBy(Pageable pageable);
/**
* A DTO projection using a constructor expression in a manually declared query.
*
* @param firstname
* @return
*/
@Query("select new example.springdata.jpa.projections.CustomerDto(c.firstname) from Customer c where c.firstname = ?1")
Collection<CustomerDto> findDtoWithConstructorExpression(String firstname);
}

View File

@@ -117,6 +117,15 @@ public class CustomerRepositoryIntegrationTest {
assertThat(((TargetAware) projectedDave).getTarget(), is(instanceOf(Map.class)));
}
@Test
public void projectsDtoUsingConstructorExpression() {
Collection<CustomerDto> result = customers.findDtoWithConstructorExpression("Dave");
assertThat(result, hasSize(1));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
}
@Test
public void supportsProjectionInCombinationWithPagination() {