#206 - Added more projection examples in JPA.

Added another one that shows projections can be wrapped into an Optional.
This commit is contained in:
Oliver Gierke
2016-08-18 15:36:01 +02:00
parent 7519f8e385
commit 34fe9d2720
2 changed files with 14 additions and 0 deletions

View File

@@ -16,6 +16,7 @@
package example.springdata.jpa.projections;
import java.util.Collection;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
@@ -104,4 +105,12 @@ public interface CustomerRepository extends CrudRepository<Customer, Long> {
*/
@Query("select new example.springdata.jpa.projections.CustomerDto(c.firstname) from Customer c where c.firstname = ?1")
Collection<CustomerDto> findDtoWithConstructorExpression(String firstname);
/**
* A projection wrapped into an {@link Optional}.
*
* @param lastname
* @return
*/
Optional<CustomerProjection> findOptionalProjectionByLastname(String lastname);
}

View File

@@ -134,4 +134,9 @@ public class CustomerRepositoryIntegrationTest {
assertThat(page.getContent().get(0).getFirstname(), is("Carter"));
}
@Test
public void appliesProjectionToOptional() {
assertThat(customers.findOptionalProjectionByLastname("Beauford").isPresent(), is(true));
}
}