From f5e6b3b6be99023f26f9ee5124df3322ec707986 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Mon, 9 Mar 2015 09:25:02 +0100 Subject: [PATCH] #62 - Add example for Java 8 Streams in JPA. This example demonstrates streaming of results in JPA. Original pull request: #63. --- .../jpa/java8/CustomerRepository.java | 13 +++++++++- .../jpa/java8/Java8IntegrationTests.java | 25 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 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 f924e9db..89f38b91 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 @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,9 @@ package example.springdata.jpa.java8; import java.util.Optional; +import java.util.stream.Stream; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.Repository; @@ -60,4 +62,13 @@ public interface CustomerRepository extends Repository { default Optional findByLastname(Customer customer) { return findByLastname(customer == null ? null : customer.lastname); } + + /** + * 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. + * + * @return + */ + @Query("select c from Customer c") + Stream streamAllCustomers(); } 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 4275ab9e..522ce152 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 @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,12 @@ package example.springdata.jpa.java8; import static org.hamcrest.CoreMatchers.*; 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; @@ -31,6 +35,7 @@ import org.springframework.transaction.annotation.Transactional; * Integration test to show the usage of Java 8 date time APIs with Spring Data JPA auditing. * * @author Oliver Gierke + * @author Thomas Darimont */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = AuditingConfiguration.class) @@ -66,4 +71,22 @@ public class Java8IntegrationTests { assertThat(result.isPresent(), is(true)); assertThat(result.get(), is(customer)); } + + /** + * Streaming data from the store by using a repsoitory 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() { + + Customer customer1 = repository.save(new Customer("Customer1", "Foo")); + Customer customer2 = repository.save(new Customer("Customer2", "Bar")); + + try (Stream stream = repository.streamAllCustomers()) { + + List customers = stream.collect(Collectors.toList()); + + assertThat(customers, IsCollectionContaining. hasItems(customer1, customer2)); + } + } }