Performance: Why relationships matter in JPA
HIGH IMPACT
This affects database query performance and page load speed by controlling how related data is fetched and rendered.
class Order {
@OneToMany(fetch = FetchType.LAZY)
List<Item> items;
}
// Items load only when accessed, reducing initial query sizeclass Order { @OneToMany(fetch = FetchType.EAGER) List<Item> items; } // Fetching orders triggers loading all items immediately, even if not needed
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager fetching all relationships | N/A (backend data) | N/A | Blocks rendering due to slow data arrival | [X] Bad |
| Lazy fetching with selective joins | N/A (backend data) | N/A | Faster data arrival, smoother rendering | [OK] Good |