The JDO Query Interface
Persistent data is permanent data that resides outside an application. But just as with direct access to any data store, you need a way to query the data store for its contents. The
JDO Query interface provides a neutral and data-store-independent query language with optimization, large-query, multi-tier, and compiled-query capabilities.
The JDO query interface has two parts:
1. The query API
2. The query language JDOQL
This section uses a typical, simple, and relevant query-based application to show you how the JDO query interface works. You can download the complete source from the References column of this article. But before going into the details of the sample program, you should know a little about the JDO query features.
JDO instances are representations of persistent data at the backend data store. The data is useful only if applications can access them and do CRUD (Create, Retrieve, Update, and Delete) operations. JDO provides three convenient instance access processes:
1. If the application code has or can construct a valid
ObjectId, it can invoke the PM's
2. The application code can call PM's
getExtent method, which returns a
Collection of all the instances of the named class and iterates through the class extent.
3. You can use
JDO Query to get a JDO instance by querying the JDO persistence manager using search criteria specified by the application.
I'll use the third approach to demonstrate and explore the JDO query mechanism. Each JDO
PersistentManager instance is a factory for
Query instances, and queries are executed in the context of the persistent manager instance. A query requires three elements:
1. The JDO class of the instances
2. The collection of candidate JDO instances
3. A query filter, which is a Java boolean expression
Optional elements in JDO query include:
Parameter declarations -
Strings containing one or more query parameter declarations separated with commas
Parameter values to bind to parameters - Java
Objects passed to the execute methods
Unbound variable declarations -
Strings containing one or more unbound variable declarations separated with semicolons
Import statements -
Strings with semicolon-separated statements
Ordering specification - A list of expressions with an ascending/descending indicator
The JDO specification provides the detailed BNF (Backus Naur Form: John Backus and Peter Naur introduced the first formal notation to describe the syntax of a given language) for JDO query.