From 65cef4132db8e04e3e5d952430ae71edd3e9b6b4 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Thu, 12 Mar 2015 10:22:11 +0100 Subject: [PATCH] #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. --- .../jpa/java8/CustomerRepository.java | 13 ++++++++-- .../jpa/java8/Java8IntegrationTests.java | 24 +++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java b/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java index 89f38b91..4231e518 100644 --- a/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java +++ b/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java @@ -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 { @@ -64,11 +65,19 @@ public interface CustomerRepository extends Repository { } /** - * 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 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 findAllByLastnameIsNotNull(); } diff --git a/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java b/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java index 522ce152..576fdc88 100644 --- a/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java +++ b/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java @@ -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 stream = repository.streamAllCustomers()) { + assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2)); + } + } - List 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. hasItems(customer1, customer2)); + Customer customer1 = repository.save(new Customer("Customer1", "Foo")); + Customer customer2 = repository.save(new Customer("Customer2", "Bar")); + + try (Stream stream = repository.findAllByLastnameIsNotNull()) { + assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2)); } } }