Java Reference
In-Depth Information
different threads, but the implementation might choose to execute them serially. In either case, the
implementation must be thread-safe.
There are three required elements in a query: the class of the results, the candidate collection of
instances, and the filter. There are optional elements: parameter declarations, variable declarations,
import statements, and an ordering specification.
The query namespace is modeled after these methods in Java:
setClass
corresponds to the class definition.
declareParameters
corresponds to formal parameters of a method.
declareVariables
corresponds to local variables of a method.
setFilter
and
setOrdering
correspond to the method body.
Note
You can find more details of these methods in the JDO document (visit
http://access1.sun.com/jdo
).
The
Query
interface provides the following methods that execute the query based on the parameters
given:
Object execute()
Object execute(Object param)
Object execute(Object[] params)
They return a
Collection
that the user can iterate to get results. For future extension, the signature of
the
execute
methods specifies that they return an
Object
that must be cast to
Collection
by the
user. Any parameters passed to the
execute
methods are used only for this execution and are not
remembered for future execution.
All queries must conform to the object query language (OQL) grammar. Unlike SQL, the JDO OQL
operates on Java classes and objects and has a strong object-oriented flavor. A JDO OQL has at least
three elements: the class of results, the JDO instances' candidate collection (usually
extent
), and the
query filter. The query filter is where you specify the query criteria. Query filters use syntax very similar
to Java syntax such as: "
name.startWith("Liberty")"
or
"getCapacity() > 12"
. The
following code snippet illustrates the use of some
Query
APIs.
…
Class target = Yacht.class;
Extent extent = pm.getExtent(target, false);
String filter = "getCapacity() > 12";
Query query = pm.newQuery(excent, filter);
Query.setClass(target);
Query.compile();
Collection result = (Collection) query.execute();
…
This piece of code searches the persistent store and returns a collection of
Yacht
objects that has a
capacity of more than 12 passengers.
Transaction Interface
Operations on persistent JDO instances at the user's choice might be performed in the context of a
transaction. That is, the view of data in the data store is transactionally consistent, according to the
standard definition of Atomicity, Consistency, Isolation, and Durability (ACID) transactions.
The
javax.jdo.Transaction
interface is used to mark the beginning and end of a application-
defined unit of work. The
PersistenceManager
allows the application to get the instance that
manages these transactional boundaries via the
currentTransaction
method.