O'Reilly logo

Java Data Objects by Craig Russell, David Jordan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating and Initializing a Query

The PersistenceManager interface contains a set of Query factory methods used to construct Query instances. They mainly differ in which query components are initialized. Query instances may be constructed at any time before a PersistenceManager is closed.

The following PersistenceManager method constructs an empty Query instance with none of the components initialized:

Query newQuery(  );

The following PersistenceManager methods construct a Query instance with an Extent as the collection of candidate instances:

Query newQuery(Extent candidates);
Query newQuery(Extent candidates, String filter);

The candidate class is initialized with the class of the Extent. The second method also initializes the query filter. We used this second method when we constructed the Query on line [3] in our example.

Alternatively, a collection can serve as the set of candidate instances in a query. The following PersistenceManager methods construct a Query instance with a Collection as the set of candidate instances:

Query newQuery(Class candidateClass, Collection candidates);
Query newQuery(Class candidateClass, Collection candidates, String filter);

When performing a query on a collection, it is necessary to specify the class of the candidate instances explicitly.

The elements in the collection should be persistent instances associated with the same PersistenceManager as the Query instance. If the collection contains instances associated with another PersistenceManager, a JDOUserException ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required