From 2c97493f9f94df979b08e2d30c63f29b1dd4388a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 9 Mar 2015 11:16:08 +0100 Subject: [PATCH] #62 - Added a section on the Stream usage to the README. Original pull request: #63. --- jpa/java8/README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/jpa/java8/README.md b/jpa/java8/README.md index 4d762d6f..ddfb9758 100644 --- a/jpa/java8/README.md +++ b/jpa/java8/README.md @@ -15,9 +15,27 @@ interface CustomerRepository extends Repository { } ``` -* `CustomerRepository.findOne(Long)` effectively "overrides" the default `CrudRepository.findOne(…)` to return `Optional.empty()` instead of `null` in case no uniqe element satisfying the query can be found. +* `CustomerRepository.findOne(Long)` effectively "overrides" the default `CrudRepository.findOne(…)` to return `Optional.empty()` instead of `null` in case no unique element satisfying the query can be found. * `CustomerRepository.findByLastname(…)` does the same for a normal query method. ## Support for JDK 8's date/time types in the auditing sub-system -* `Customer` extends `AbstractEntity` which contains fields of JDK 8's new date/time API and those can be populated the same way legacy types like `Date` and `Calendar` can. \ No newline at end of file +* `Customer` extends `AbstractEntity` which contains fields of JDK 8's new date/time API and those can be populated the same way legacy types like `Date` and `Calendar` can. + +## Support for JDK 8' `Stream` in repository methods + +JPA repositories can now use `Stream` as return type for query methods to trigger streamed execution of the query. This will cause Spring Data JPA to use persistnce provider specific API to traverse the query result one-by-one. + +```java +interface CustomerRepository extends Repository { + + @Query("select c from Customer c") + Stream streamAllCustomers(); +} + +try (Stream customers = repository.streamAllCustomers()) { + // use the stream here +} +``` + +Note how the returned `Stream` has to be used in a try-with-resources clause as the underlying resources have to be closed once we finished iterating over the result. \ No newline at end of file