An open API service indexing awesome lists of open source software.

https://github.com/daggerok/spring-data-java8

Java 8 CompletableFuture, Stream API return types for spring-data repositories
https://github.com/daggerok/spring-data-java8

completable-future completablefuture spring-data-java8 spring-data-jpa stream stream-api streamable

Last synced: 3 months ago
JSON representation

Java 8 CompletableFuture, Stream API return types for spring-data repositories

Awesome Lists containing this project

README

          

# spring-data-java8
Java 8 CompletableFuture, Stream API return types for spring-data (JPA) repositories

```java
@Repository
interface MessageRepository extends JpaRepository {

Streamable streamAllBy();

/*
must be executed inside transaction, otherwise you will get this error:

org.springframework.dao.InvalidDataAccessApiUsageException: You're trying to execute a streaming
query method without a surrounding transaction that keeps the connection open so that the Stream can
actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of
declaring a (read-only) transaction.

you can use TransactionTemplate for that, like so:

return transactionTemplate.execute(status -> {
try (Stream stream = messageRepository.findAnyBy()) {
return stream.collect(toList());
}
});
*/
Stream findAnyBy();

/*
you may want to configure and use custom taskExecutor:

@Bean
@Qualifier("myFutures")
public TaskExecutor taskExecutor() {
return new SimpleAsyncTaskExecutor("my-futures");
}
*/
@Async("myFutures")
CompletableFuture> findAllBy();
}
```

links:

- [read more](https://docs.spring.io/spring-data/data-jpa/docs/2.2.x/reference/html)