Whenever you have a repository extending CrudRepository such as:
@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
//something
}
and you want to use the method findAll()
:
Iterable<T> findAll();
You will get an iterable. Obviously, I could convert the Iterable into a list, like this:
public List<Employee> findAll() {
List<Employee> employees = new ArrayList<>();
employeeRepository.findAll().forEach(employees::add);
return students;
}
Or I could Override the findAll()
method on the repository or even I could simply go with JpaRepository
instead, but my questions are:
- 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?
- 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.