What is the purpose of findAll() returning Iterable<T> in CrudRepository interface
07:58 07 Mar 2021

Whenever you have a repository extending CrudRepository such as:

@Repository
public interface EmployeeRepository extends CrudRepository {
    //something 
}

and you want to use the method findAll():

Iterable findAll();

You will get an iterable. Obviously, I could convert the Iterable into a list, like this:

public List findAll() {
    List employees = new ArrayList<>();
    employeeRepository.findAll().forEach(employees::add);
    return employees;
}

Or I could Override the findAll() method on the repository or even I could simply go with JpaRepository instead, but my questions are:

  1. What is the purpose of getting an Iterable in the first place? By converting it into a List am I going against the whole purpose of using CrudRepository?
  2. If indeed is a fine approach to convert the Iterable into a List. What is the most efficient way to achieve this?

Hope somebody can give me some insight on this.

spring-boot iterable crud-repository