Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA Repositories with Java 8 Streams

Lets say I have the following repository:

public interface UserRepository extends JpaRepository<User, Long> {

    @Query("select u from User u")
    Stream<User> streamAllPaged(Pageable pageable);
}

And I want to perform a search:

public Page<User> findAllUsers(Pageable page) {

    Page<User> page = null;
    try (Stream<User> users = userRepository.streamAllPaged(page)) {
            Set<User> users = users.filter(u -> user.getName().equals("foo"))
                    .collect(Collectors.toSet());
            //create page from set?
    }

}

Obviously I could use a sublist and manually insert page sizes etc but I guess that there should be a more "standard" way to do this?

like image 863
ChrisGeo Avatar asked Nov 03 '16 09:11

ChrisGeo


1 Answers

I'd argue your use case doesn't make too much sense here. If you want to end up with a Page of results eventually, starting with a Stream is just inefficient. You could easily achieve your expected end result with this:

public interface UserRepository extends CrudRepository<User, Long> {

  Page<User> findByName(String name, Pageable pageable);
}

That'd make sure you only read the amount of names you initially request. Using a Stream here is completely subverting the point as filtering on the Stream will require all User instances to be loaded into memory just to invoke the predicate. You definitely want to let the database do that only return the values that match in the first place.

like image 113
Oliver Drotbohm Avatar answered Oct 07 '22 17:10

Oliver Drotbohm