Skip to content

Commit 34fe9d2

Browse files
committed
spring-projects#206 - Added more projection examples in JPA.
Added another one that shows projections can be wrapped into an Optional.
1 parent 7519f8e commit 34fe9d2

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package example.springdata.jpa.projections;
1717

1818
import java.util.Collection;
19+
import java.util.Optional;
1920

2021
import org.springframework.beans.factory.annotation.Value;
2122
import org.springframework.data.domain.Page;
@@ -104,4 +105,12 @@ public interface CustomerRepository extends CrudRepository<Customer, Long> {
104105
*/
105106
@Query("select new example.springdata.jpa.projections.CustomerDto(c.firstname) from Customer c where c.firstname = ?1")
106107
Collection<CustomerDto> findDtoWithConstructorExpression(String firstname);
108+
109+
/**
110+
* A projection wrapped into an {@link Optional}.
111+
*
112+
* @param lastname
113+
* @return
114+
*/
115+
Optional<CustomerProjection> findOptionalProjectionByLastname(String lastname);
107116
}

‎jpa/example/src/test/java/example/springdata/jpa/projections/CustomerRepositoryIntegrationTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,9 @@ public void supportsProjectionInCombinationWithPagination() {
134134

135135
assertThat(page.getContent().get(0).getFirstname(), is("Carter"));
136136
}
137+
138+
@Test
139+
public void appliesProjectionToOptional() {
140+
assertThat(customers.findOptionalProjectionByLastname("Beauford").isPresent(), is(true));
141+
}
137142
}

0 commit comments

Comments
 (0)