The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption.

Noncompliant Code Example

private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

List<Employee> employees = new ArrayList<>();

for (Integer id: ids) {
    Optional<Employee> employee = employeeRepository.findById(id); // Noncompliant
    if (employee.isPresent()) {
        employees.add(employee.get());
    }
}

Compliant Solution

private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Employee> employees = employeeRepository.findAllById(ids);