Chapter 11. Query

11.1. Required Query Elements
11.2. Optional Query Elements
11.3. JDOQL
11.4. Executing Queries
11.5. Query Compilation

You can obtain Query instances from a PersistenceManager. They are used to filter a set of candidate objects based on certain criteria. This filtering might take place in the data store, or might be executed in memory. JDO does not mandate any one query mechanism, and most implementations probably use a mixture of datastore and in-memory execution depending on the circumstances.

We will now explore the elements of the Query interface. You might find yourself scratching your head over some aspects of the discussion below; this is to be expected. All will be made clear when we review several examples of JDO queries later in this chapter.

11.1. Required Query Elements

Every Query has three required elements:

  • The candidate class. Only instances of this class and its subclasses will be considered when evaluating the query filter. The candidate class is set through the setClass method.

  • The set of candidate objects. Candidates can be specified as either a Collection of objects or as an Extent. There are setCandidate methods to handle both cases. If an Extent is given, then you do not need to separately specify a candidate class for the query; the query will inherit the Extent's candidate class.

  • The filter string. This is a String written in the JDO Query Language (JDOQL) and set via the setFilter method. If you do not specify a filter, all objects in the candidate set that are assignable to the candidate class will match the query.