![]() | ![]() |
![]() |
| JPOX | Version 1.0 | Version 1.1 | Version 1.2 | JDO | JPA |
| 1.2 | Persistence | JDO ORM | JPA ORM | Query | Runtime | Extensions | Tutorials and Examples |
![]() As we have seen, a JDOQL query is made up of different parts. In this section we look at the result part of the query. The result is what we want returning. By default (when not specifying the result) the objects returned will be of the candidate class type, where they match the query filter. Firstly let's look at what you can include in the result clause.
The result is specified in JDOQL like this query.setResult("count(field1), field2");In Single-String JDOQL you would specify it directly.
What you specify in the result defines what form of result you get back.
![]() There are situations when you want to return a single number for a column, representing an aggregate of the values of all records. There are 5 standard JDO2 aggregate functions available. These are
So to utilise these you could specify something like
Query q = pm.newQuery("SELECT max(price), min(price) FROM org.jpox.samples.store.Product WHERE status == 1");This will return a single row of results with 2 values, the maximum price and the minimum price of all products that have status code of 1.
![]() JPOX's JDOQLQuery can provide meta data for the results of a JDOQL query before its execution. It can return a JDOQLResutSetMetaData that holds information about the number and Java-types of the returned columns of the associated query. Note that the query doesn't need to be executed but still compiled before retrieving its meta data. Here is an example usage:
Query q = pm.newQuery("SELECT this.name, sum(this.price) FROM org.jpox.samples.store.Product GROUP BY this.name");
q.compile();
JDOQLResultSetMetaData metaData = ((JDOQLQuery)q).getResultSetMetaData();
for (int i = 0; i < metaData.getExpressionCount(); i++)
{
System.out.println("Column "+i+" is of type "+metaData.getExpressionType(i));
}
JDO 2 introduces the ability to use aggregates in queries. Here's another example using the same Product class as above, but this time looking for the maximum price of products that are CD Players. Note that the result for this particular query will be of type Double since there is a single double precision value being returned via the "result".
Declarative JDOQL :
Query query = pm.newQuery(org.jpox.samples.store.Product.class);
query.setFilter("name == \"CD Player\"");
query.setResult("max(this.price)");
List results = (List)query.execute();
Iterator iter = c.iterator();
Double max_price = (Double)iter.next();
Single-String JDOQL :
Query query = pm.newQuery("SELECT max(price) FROM org.jpox.samples.store.Product WHERE name == \"CD Player\"");
List results = (List)query.execute();
Iterator iter = c.iterator();
Double max_price = (Double)iter.next(); |