Skip to content

Commit 65cef41

Browse files
Thomas Darimontodrotbohm
authored andcommitted
spring-projects#66 - Added JPA example for using Java 8 Streams with a derived query.
This example demonstrates the usage of Java 8 Stream as return type for derived queries.
1 parent 2569cf8 commit 65cef41

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Repository to manage {@link Customer} instances.
2727
*
2828
* @author Oliver Gierke
29+
* @author Thomas Darimont
2930
*/
3031
public interface CustomerRepository extends Repository<Customer, Long> {
3132

@@ -64,11 +65,19 @@ default Optional<Customer> findByLastname(Customer customer) {
6465
}
6566

6667
/**
67-
* Sample method to demonstrate support for {@link Stream} as a return type. The query is executed in a streaming
68-
* fashion which means that the method returns as soon as the first results are ready.
68+
* Sample method to demonstrate support for {@link Stream} as a return type with a custom query. The query is executed
69+
* in a streaming fashion which means that the method returns as soon as the first results are ready.
6970
*
7071
* @return
7172
*/
7273
@Query("select c from Customer c")
7374
Stream<Customer> streamAllCustomers();
75+
76+
/**
77+
* Sample method to demonstrate support for {@link Stream} as a return type with a derived query. The query is
78+
* executed in a streaming fashion which means that the method returns as soon as the first results are ready.
79+
*
80+
* @return
81+
*/
82+
Stream<Customer> findAllByLastnameIsNotNull();
7483
}

jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
*/
1616
package example.springdata.jpa.java8;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020

21-
import java.util.List;
2221
import java.util.Optional;
2322
import java.util.stream.Collectors;
2423
import java.util.stream.Stream;
2524

26-
import org.hamcrest.core.IsCollectionContaining;
2725
import org.junit.Test;
2826
import org.junit.runner.RunWith;
2927
import org.springframework.beans.factory.annotation.Autowired;
@@ -73,20 +71,32 @@ public void invokesDefaultMethod() {
7371
}
7472

7573
/**
76-
* Streaming data from the store by using a repsoitory method that returns a {@link Stream}. Note, that since the
74+
* Streaming data from the store by using a repository method that returns a {@link Stream}. Note, that since the
7775
* resulting {@link Stream} contains state it needs to be closed explicitly after use!
7876
*/
7977
@Test
80-
public void useJava8StreamsDirectly() {
78+
public void useJava8StreamsWithCustomQuery() {
8179

8280
Customer customer1 = repository.save(new Customer("Customer1", "Foo"));
8381
Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
8482

8583
try (Stream<Customer> stream = repository.streamAllCustomers()) {
84+
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
85+
}
86+
}
87+
88+
/**
89+
* Streaming data from the store by using a repository method that returns a {@link Stream} with a derived query.
90+
* Note, that since the resulting {@link Stream} contains state it needs to be closed explicitly after use!
91+
*/
92+
@Test
93+
public void useJava8StreamsWithDerivedQuery() {
8694

87-
List<Customer> customers = stream.collect(Collectors.toList());
95+
Customer customer1 = repository.save(new Customer("Customer1", "Foo"));
96+
Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
8897

89-
assertThat(customers, IsCollectionContaining.<Customer> hasItems(customer1, customer2));
98+
try (Stream<Customer> stream = repository.findAllByLastnameIsNotNull()) {
99+
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
90100
}
91101
}
92102
}

0 commit comments

Comments
 (0)