ORM N+1 query (ARCH4079)
Esta página aún no está disponible en tu idioma.
Key java.performance.orm-n-plus-one · Severity error · Category performance
Accessing a lazy relationship on each row of a query result triggers a separate query per relationship. This issues N+1 queries instead of one, overwhelming the database.
Incorrect
Section titled “Incorrect”List<Order> orders = repo.findAll();for (Order o : orders) { total += o.getCustomer().getName().length(); // lazy fetch per row}Correct
Section titled “Correct”List<Order> orders = repo.findAllWithCustomer(); // JOIN FETCH customerfor (Order o : orders) { total += o.getCustomer().getName().length();}Eager-load the relationship with JOIN FETCH or an entity graph so it is fetched in one query.