Type: New Feature
Affects Version/s: None
Fix Version/s: None
In JPA an often recurring problem is how the user tells the persistence provider to which depth an object graph should be fetched.
Lazy- and eager loading is one such method, but these are structural directives. Queries in combination with JOIN FETCH is a more dynamic solution, but having to write variants of a given query for each particular fetch depth and width is non-optimal.
To ease the task of letting the user specify what part of the object graph should be fetched, I would like to propose introducing the concept of simple path expressions. Such path expressions specify which transitive relations from a query root should be eagerly loaded.
In the above example, both the "address" and the "friends" relation of entity User are being eagerly loaded, and in addition the "address" relation of each element in the "friends" relation is loaded.
The persistence provider should be free to optimize how the intent of the user is realized. E.g. if SQL is generated that uses a JOIN, if separate queries are executed, or perhaps something else entirely.
As an optional part of this proposal, a syntax could be introduced which does let the user specify the most common fetching strategies. E.g. -> for a join and # for a separate query.
Using such syntax, the above code would look like the following:
In the above example, the persistence provider would try to create a query that joins the tables associated with the User and address, and then a separate query (or queries) that joins the tables associated with friends and address.