Hibernate / EclipseLink Interview questions
How does EclipseLink decide when to use join fetch versus batch fetch?
Neither strategy is chosen automatically by EclipseLink on its own; both are opt-in through explicit query hints (eclipselink.join-fetch or eclipselink.batch), and the right choice depends on the shape of the data being fetched.
| Join fetch | Batch fetch |
| Fetches the relationship in the same SQL query, via an outer join. | Fetches the relationship in one additional, separate query after the main result set. |
| Best for to-one relationships or small, bounded collections. | Best for to-many relationships, especially large or unpredictable collection sizes. |
| Risk of a Cartesian-product-style row explosion with large to-many collections. | Avoids row multiplication, since it's a separate query keyed by parent IDs. |
| One round-trip total. | Two round-trips total (main query plus one batch query), but each is smaller. |
A practical rule of thumb: use join fetch for @ManyToOne/@OneToOne relationships accessed alongside the parent, since there's no multiplication risk there, and reach for batch fetch on @OneToMany/@ManyToMany collections instead, where a join would otherwise multiply the parent row once per child row returned.
More Related questions...