JPA Criteria FROM and JOIN

The Jakarta Persistence Criteria API uses a specific set of interfaces to construct the FROM clause of a query. These interfaces define query variables, including entity roots and various join types, to manage data navigation and retrieval optimization.

The interface hierarchy for the FROM clause is structured as follows:

Criteria Query From Components in Jakarta Persistence (JPA) 3.2 

Criteria query variables

The FORM clause in queries defines query variables, which act as loop variables for iterating over data in the database that may be relevant to the query results.

Query variables are represented in the Criteria API by subinterfaces of the abstract From interface:

Defines an entity type variable, whose scope is all instances of the entity type.

Defines an attribute variable, whose scope is all values of a specific persistent attribute.

The base interface for joining to a collection attribute (one-to-many or many-to-many associations).

Represents a join to an association typed as a java.util.Collection.

Represents a join to an association typed as a java.util.Set.

Represents a join to an association typed as a java.util.List. It provides support for list indices.

Represents a join to an association typed as a java.util.Map. It provides access to both map keys and values.

The parent interface of Root and Join, defining the common for both, such the variable type.

An enumeration defining the type of join to perform: INNER (default) or LEFT.

JOIN FETCH virtual variables

Fetch joins optimize performance by pre-fetching specified related data with query results.

 The following interfaces manage fetch operations within the Criteria API:

Defines a virtual anonymous join variable,  whose scope is all values of a specific persistent attribute. Similar to a regular join it functions as a loop that iterates over the attribute values. Unlike a standard join, these values cannot be used in the query results or in other query components other than in additional fetches. The only purpose is pre-fetching of these values (typically other referenced entities) to make working with the query results more efficient.

FetchParent serves as the base for objects that can initiate these fetch operations, such as From and other Fetch instances.

For more details and an example, see the FROM in Criteria Queries section.