3.14.2.3. Distributed Query
The queries are executed in parallel across one or more slices and the individual query results are merged into a single list before being returned to the caller application. The merge operation is more complex for the queries that involve sorting and/or specify a range. Slice supports both sorting and range queries.
Slice also supports aggregate queries where the aggregate operation is commutative to partitioning such as COUNT() or MAX() but not AVG().
By default, any query is executed against all available slices.
However, the application can target the query only to a subset of slices by setting hint on jakarta.persistence.Query.
The hint key is openjpa.hint.slice.Target and hint value is an array of slice identifiers.
The following example shows how to target a query only to a pair of slices with logical identifier "One" and "Two".
EntityManager em = ...;
em.getTransaction().begin();
String hint = "openjpa.hint.slice.Target";
Query query = em.createQuery("SELECT p FROM PObject")
.setHint(hint, new String[]{"One", "Two"});
List result = query.getResultList();
// verify that each instance is originating from the hinted slices
for (Object pc : result) {
String sliceOrigin = SlicePersistence.getSlice(pc);
assertTrue ("One".equals(sliceOrigin) || "Two".equals(sliceOrigin));
}
The target hint applies to bulk UPDATE and DELETE statements, in JPQL and in native SQL, in the same way as to queries: such a statement runs only on the hinted slices.
If the hint names no active slice, the query fails with an exception instead of running on all slices.
To confine queries to a subset of slices via setting query hints can be considered intrusive to existing application.
The alternative means of targeting queries is to configure a Query Target Policy.
This policy is configured via plug-in property openjpa.slice.QueryTargetPolicy.
The plug-in property is fully-qualified class name of an implementation for org.apache.openjpa.slice.QueryTargetPolicy interface.
This interface contract allows a user application to target a query to a subset of slices based on the query and its bound parameters.
The query target policy is consulted only when no explicit target hint is set on the query.
It is consulted for bulk UPDATE and DELETE statements as well.
By default, the policy executes a query on all available slices.
A similar policy interface org.apache.openjpa.slice.FinderTargetPolicy is available to target queries that originate from find() by primary key.
This finder target policy is consulted only when no explicit target hint is set on the current fetch plan.
By default, the policy executes a query on all available slices to find an instance by its primary key.