Skip to content

Commit f5e6b3b

Browse files
Thomas Darimontodrotbohm
authored andcommitted
spring-projects#62 - Add example for Java 8 Streams in JPA.
This example demonstrates streaming of results in JPA. Original pull request: spring-projects#63.
1 parent 49df946 commit f5e6b3b

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,9 @@
1616
package example.springdata.jpa.java8;
1717

1818
import java.util.Optional;
19+
import java.util.stream.Stream;
1920

21+
import org.springframework.data.jpa.repository.Query;
2022
import org.springframework.data.repository.CrudRepository;
2123
import org.springframework.data.repository.Repository;
2224

@@ -60,4 +62,13 @@ public interface CustomerRepository extends Repository<Customer, Long> {
6062
default Optional<Customer> findByLastname(Customer customer) {
6163
return findByLastname(customer == null ? null : customer.lastname);
6264
}
65+
66+
/**
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.
69+
*
70+
* @return
71+
*/
72+
@Query("select c from Customer c")
73+
Stream<Customer> streamAllCustomers();
6374
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,12 @@
1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020

21+
import java.util.List;
2122
import java.util.Optional;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
2225

26+
import org.hamcrest.core.IsCollectionContaining;
2327
import org.junit.Test;
2428
import org.junit.runner.RunWith;
2529
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,6 +35,7 @@
3135
* Integration test to show the usage of Java 8 date time APIs with Spring Data JPA auditing.
3236
*
3337
* @author Oliver Gierke
38+
* @author Thomas Darimont
3439
*/
3540
@RunWith(SpringJUnit4ClassRunner.class)
3641
@ContextConfiguration(classes = AuditingConfiguration.class)
@@ -66,4 +71,22 @@ public void invokesDefaultMethod() {
6671
assertThat(result.isPresent(), is(true));
6772
assertThat(result.get(), is(customer));
6873
}
74+
75+
/**
76+
* Streaming data from the store by using a repsoitory method that returns a {@link Stream}. Note, that since the
77+
* resulting {@link Stream} contains state it needs to be closed explicitly after use!
78+
*/
79+
@Test
80+
public void useJava8StreamsDirectly() {
81+
82+
Customer customer1 = repository.save(new Customer("Customer1", "Foo"));
83+
Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
84+
85+
try (Stream<Customer> stream = repository.streamAllCustomers()) {
86+
87+
List<Customer> customers = stream.collect(Collectors.toList());
88+
89+
assertThat(customers, IsCollectionContaining.<Customer> hasItems(customer1, customer2));
90+
}
91+
}
6992
}

0 commit comments

Comments
 (0)