db-jdo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig L Russell <Craig.Russ...@Sun.COM>
Subject Subquery specification update
Date Wed, 24 Oct 2007 20:14:21 GMT
Here's the specification update for addSubquery that was missing from  
the previous version.

Subqueries
void addSubQuery (Query subquery, String variableDeclaration,
	String candidateCollectionExpression);
This method adds a subquery to this Query. A subquery is composed as  
a Query and subsequently attached to a different Query by calling  
this method. The Query parameter instance is unmodified as a result  
of the addSubquery or subsequent execution of the outer Query. Only  
some of the query parts are copied for use as the subquery. The parts  
copied include the candidate class, filter, parameter declarations,  
variable declarations, imports, ordering specification, uniqueness,  
result specification, and grouping specification. The association  
with a PersistenceManager, the candidate collection, result class,  
limits on size, and number of skipped instances are not used.

The variableDeclaration is the name of the variable containing the  
results of the subquery execution. If the same value of  
variableDeclaration is used to add multpile subqueries, the subquery  
replaces the previous subquery for the same variableDeclaration. If  
the subquery parameter is null, the variableDeclaration is unset,  
effectively making the variable named in the variableDeclaration  
unbound.

The candidateCollectionExpression is the expression using tokens from  
this query that represent the candidates over which the subquery is  
evaluated.

For example, to find employees who work more than the average of all  
employees,
Query sub = pm.newQuery(Employee.class);
sub.setResult("avg(this.weeklyhours)");
Query q = pm.newQuery(Employee.class);
q.setFilter("this.weeklyHours > averageWeeklyhours");
q.addSubquery(sub, "double averageWeeklyhours", null);

Correlated subqueries
A correlated subquery is a subquery which contains references to  
fields in the outer query. If the correlation can be expressed as a  
restriction of the candidate collection of the subquery, no  
parameters are needed.

For example, to find employees who work more than the average of  
their department employees:
Query sub = pm.newQuery(Employee.class);
sub.setResult("avg(this.weeklyhours)");
Query q = pm.newQuery(Employee.class);
q.setFilter("this.weeklyhours> averageWeeklyhours");
q.addSubquery(sub, "double averageWeeklyhours",  
"this.department.employees");

If the correlation cannot be expressed as a restriction of the  
candidate collection, the correlation is expressed as one or more  
parameters in the subquery which are bound to expressions of the  
outer query.
void addSubquery(String variableDeclaration,
	Query subquery, String candidateCollectionExpr, String parameter);
void addSubquery(String variableDeclaration,
	Query subquery, String candidateCollectionExpr, String[] parameters);
void addSubquery(String variableDeclaration,
	Query subquery, String candidateCollectionExpr, Map parameters);
The parameters parameter in the above methods binds parameters in the  
subquery to expressions in the outer query.

For example, to find employees who work more than the average of the  
employees in their department having the same manager:
Query sub = pm.newQuery(Employee.class);
sub.setResult("avg(this.weeklyhours)");
sub.setFilter("this.manager == :manager");
Query q = pm.newQuery(Employee.class);
q.setFilter("this.weeklyHours > averageWeeklyhours");
q.addSubquery(sub, "double averageWeeklyhours",  
"this.department.employees", "this.manager");
The parameter in the subquery “:manager” is bound to the expression  
“this.manager” in the context of the outer query.

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Mime
View raw message