Lazy Load In Loop (ARCH4059)
Ce contenu n’est pas encore disponible dans votre langue.
Key java.orm.lazy-load-in-loop · Severity warning · Category orm
Accessing a lazy association on every loop iteration triggers N+1 when eager loading was omitted. Eager-load the association before the loop.
Incorrect
Section titled “Incorrect”List<Department> deps = departmentRepository.findAll();for (Department d : deps) { int count = d.getEmployees().size(); // lazy SELECT each iteration}Correct
Section titled “Correct”@EntityGraph(attributePaths = "employees")List<Department> findAll();
List<Department> deps = departmentRepository.findAll();for (Department d : deps) { int count = d.getEmployees().size();}Preload the relation touched in the loop with @EntityGraph or JOIN FETCH.