Chapter 11. Query
Prev | Part II. Java Data Objects | Next

## Chapter 11. Query

You can obtain `Query` instances from a `PersistenceManager`. Queries 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. We will clarify everything with several concrete examples of JDO queries later in this chapter.

## 11.1. Required Query Elements

    public void setClass (Class candidateClass);
    public void setCandidates (Extent candidates);
    public void setCandidates (Collection candidates);
    public void setFilter (String filter);

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.

Prev | Up | Next
Chapter 10. Extent | Home | 11.2. Optional Query Elements