142 Queries with JDOQL Now that it has
142 Queries with JDOQL Now that it has multiple parameters, the query execution can be rewritten to show the meaningful use of named parameters. This is particularly useful when the code executing the query is separate from the code constructing the query and requesting parameter values. Here we use a HashMap to hold the parameters: Extent partnerExt = pm.getExtent(BusinessPartner.class, true); String filter = “name == searchName && address == searchAddress”; Query q = pm.newQuery(partnerExt, filter); q.declareParameters(”String searchName, String searchAddress”); q.setOrdering(”name ascending, partnerId descending”); Map m = new HashMap(); m.add(”searchAddress”, “Milton Keynes”); m.add(”searchName”, “Ogilvie Partners”); Collection c = (Collection) q.executeWithMap(m); Notice that the order in which parameters are added to the map is insignificant. I have deliberately shown the reverse order being used to reinforce this concept. 8.6.2 Singleton field navigation The next item to illustrate is the de-referencing of persistent fields that refer to other persistent objects. Let s write a query that retrieves all business partners with a credit limit greater than a parameter value. Notice that the credit limit is stored on the Customer instance. Extent partnerExt = pm.getExtent(BusinessPartner.class, true); String filter = “customer.creditLimit > searchCredit”; Query q = pm.newQuery(partnerExt, filter); q.declareParameters(”double searchCredit”); Double credit = new Double(1000); Collection c = (Collection) q.execute(credit); The above query de-references a singleton field. The customer field either references a single Customer object, or it is null. If the field is null, then attempts to de-reference it will cause that filter subexpression to evaluate to false. Since there is only one such subexpression, the filter will evaluate to false. Thus, BusinessPartners that are not Customers will be excluded from the query results. 8.6.3 Collection field navigation Our final query example illustrates navigation through collections. Here we identify the list of BusinessPartners that have at least one Order that has not yet been dispatched. This requires the definition of a variable to reference each Order that is examined.
Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider