تخطَّ إلى المحتوى

ORM N+1 query (ARCH4079)

هذا المحتوى غير متوفر بلغتك بعد.

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.

List<Order> orders = repo.findAll();
for (Order o : orders) {
total += o.getCustomer().getName().length(); // lazy fetch per row
}
List<Order> orders = repo.findAllWithCustomer(); // JOIN FETCH customer
for (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.