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 3ee7ffa3..4031ab6f 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 @@ -95,4 +95,13 @@ public interface CustomerRepository extends CrudRepository { * @return */ Page 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 findDtoWithConstructorExpression(String firstname); } 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 44a64723..ca20e9cc 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 @@ -117,6 +117,15 @@ public class CustomerRepositoryIntegrationTest { assertThat(((TargetAware) projectedDave).getTarget(), is(instanceOf(Map.class))); } + @Test + public void projectsDtoUsingConstructorExpression() { + + Collection result = customers.findDtoWithConstructorExpression("Dave"); + + assertThat(result, hasSize(1)); + assertThat(result.iterator().next().getFirstname(), is("Dave")); + } + @Test public void supportsProjectionInCombinationWithPagination() {