#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.
This commit is contained in:
Thomas Darimont
2015-03-12 10:22:11 +01:00
committed by Oliver Gierke
parent 2569cf8de3
commit 65cef4132d
2 changed files with 28 additions and 9 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.data.repository.Repository;
* Repository to manage {@link Customer} instances.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public interface CustomerRepository extends Repository<Customer, Long> {
@@ -64,11 +65,19 @@ public interface CustomerRepository extends Repository<Customer, Long> {
}
/**
* Sample method to demonstrate support for {@link Stream} as a return type. The query is executed in a streaming
* fashion which means that the method returns as soon as the first results are ready.
* Sample method to demonstrate support for {@link Stream} as a return type with a custom query. The query is executed
* in a streaming fashion which means that the method returns as soon as the first results are ready.
*
* @return
*/
@Query("select c from Customer c")
Stream<Customer> streamAllCustomers();
/**
* Sample method to demonstrate support for {@link Stream} as a return type with a derived query. The query is
* executed in a streaming fashion which means that the method returns as soon as the first results are ready.
*
* @return
*/
Stream<Customer> findAllByLastnameIsNotNull();
}

View File

@@ -15,15 +15,13 @@
*/
package example.springdata.jpa.java8;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.hamcrest.core.IsCollectionContaining;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -73,20 +71,32 @@ public class Java8IntegrationTests {
}
/**
* Streaming data from the store by using a repsoitory method that returns a {@link Stream}. Note, that since the
* Streaming data from the store by using a repository method that returns a {@link Stream}. Note, that since the
* resulting {@link Stream} contains state it needs to be closed explicitly after use!
*/
@Test
public void useJava8StreamsDirectly() {
public void useJava8StreamsWithCustomQuery() {
Customer customer1 = repository.save(new Customer("Customer1", "Foo"));
Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
try (Stream<Customer> stream = repository.streamAllCustomers()) {
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
}
}
List<Customer> customers = stream.collect(Collectors.toList());
/**
* Streaming data from the store by using a repository method that returns a {@link Stream} with a derived query.
* Note, that since the resulting {@link Stream} contains state it needs to be closed explicitly after use!
*/
@Test
public void useJava8StreamsWithDerivedQuery() {
assertThat(customers, IsCollectionContaining.<Customer> hasItems(customer1, customer2));
Customer customer1 = repository.save(new Customer("Customer1", "Foo"));
Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
try (Stream<Customer> stream = repository.findAllByLastnameIsNotNull()) {
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
}
}
}