Hibernate / EclipseLink Interview questions
Why use fetch groups instead of always fetching the full entity?
Loading every mapped attribute of an entity on every query, including large text/BLOB columns or rarely-needed relationships, wastes both database bandwidth and memory when most of that data goes unused for a given operation. Fetch groups let a query specify only what it actually needs.
This matters most for entities with expensive-to-load attributes: a document entity with a large binary attachment field, or an entity with dozens of columns where a given screen only displays five of them. Restricting the fetch group to those five columns means the query, the network transfer, and the object's memory footprint are all proportionally smaller.
The tradeoff is that accessing an attribute outside the configured fetch group triggers a second, on-demand query (assuming weaving is active), so fetch groups are best applied where the excluded attributes genuinely aren't needed for that use case; using them aggressively on data that ends up getting accessed anyway just trades one large query for several smaller ones, which is usually a net loss rather than a win.
More Related questions...