#206 - Added more projection examples in JPA.
Added another one that shows projections can be wrapped into an Optional.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user