Write Inside Loop (ARCH4052)
Ce contenu n’est pas encore disponible dans votre langue.
Key java.orm.write-inside-loop · Severity error · Category orm
Calling save/flush on every loop iteration produces a separate DB round-trip per record. Accumulate the writes and flush once outside the loop.
Incorrect
Section titled “Incorrect”for (User user : users) { user.setActive(true); userRepository.save(user); // flush each iteration -> N round-trips}Correct
Section titled “Correct”for (User user : users) { user.setActive(true);}userRepository.saveAll(users); // single batchMove the persist call out of the loop and apply it once via saveAll/batch flush.