11.9. Single-String JDOQL

It is often convenient to represent an entire JDOQL query as a single
string. JDO defines a single-string query form that encompasses all
parts of a query: the unique flag, result string, result class,
filter, parameters, variables, imports, grouping, ordering, and range.

The PersistenceManager has a factory
method designed specifically for constructing a query from a JDOQL
string:

public Query newQuery (String query);

Additionally, any of the PersistenceManager's
query factory methods that accept a JDOQL filter string will also
accept the JDOQL single-string format. See
Section 8.11, “Query Factory”.

In each code block below, we create and execute a standard JDOQL query,
then demonstrate the same process using the single-string format.
As you'll see, translating any query into single-string form is just a
matter of plugging the corresponding strings into the grammar above.

On a final note, single-string queries are mutable. You can
construct a single-string query as in the examples above, then change
the result string, filter, or any other part of the query through the
standard Query APIs. This is especially useful
for temporary properties like the result range, and for setting the
candidate class: