With the JDO2 API you can either define a query at runtime, or define it in the MetaData/annotations for a class and
refer to it at runtime using a symbolic name. This second option means that the method of invoking the query at runtime
is much simplified. To demonstrate the process, lets say we have a class called
(something to sell in a store).
We define the JDO Meta-Data for the class in the normal way, but we also have some query that we know we will require,
so we define the following in the Meta-Data.
<query name="SoldOut" language="javax.jdo.query.JDOQL"><![CDATA[
SELECT FROM org.datanucleus.example.Product WHERE status == "Sold Out"
So we have a JDOQL query called "SoldOut" defined for the class
that returns all Products (and subclasses)
that have a
of "Sold Out". Out of interest, what we would then do in our application to execute this
query woule be
The above example was for the JDOQL object-based query language. We can do a similar thing using SQL, so we define
the following in our MetaData for our
<query name="PriceBelowValue" language="javax.jdo.query.SQL"><![CDATA[
SELECT NAME FROM PRODUCT WHERE PRICE < ?
So here we have an SQL query that will return the names of all Products that have a price less than a specified
value. This leaves us the flexibility to specify the value at runtime. So here we run our named query, asking for the
names of all Products with price below 20 euros.
All of the examples above have been specifed within the <class> element of the MetaData. You can, however,
specify queries below <jdo> in which case the query is not scoped by a particular candidate class. In this case
you must put your queries in any of the following MetaData files
Please proceed to the sections specific to
for details on the precise nature of the query for the querying languages
supported by DataNucleus.