How to use streams in spring jparepository.
I am using follwing way
customerrepository.java
public interface CustomerRepository extends JpaRepository<Customer, Long> {
@Query("select c from Customer c")
Stream<Customer> getAllFirstNames();
}
vaddinui.java
@Transactional
@RequestMapping( method = RequestMethod.GET)
public Stream<Customer> podItems() {
System.out.println("enter into method");
try (Stream<Customer> customers = repo.getAllFirstNames()){
System.out.println("enter into try block");
customers.collect(Collectors.toList()).contains(repo.findAll());
return customers;
}finally{
System.out.println("finallyblock");
}
}
whenever i call that method i got error like
**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.**