Skip to content

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

Notifications You must be signed in to change notification settings

daggerok/spring-data-java8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-data-java8

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

@Repository
interface MessageRepository extends JpaRepository<Message, UUID> {

  Streamable<Message> 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<Message> stream = messageRepository.findAnyBy()) {
         return stream.collect(toList());
       }
     });
  */
  Stream<Message> findAnyBy();

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

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

links:

Releases

No releases published

Packages

No packages published

Languages