140 Queries with JDOQL 8.5.3.2. Equality The equality

Further examples 141 8.6 Further examples Now that you know a bit more about JDOQL s capabilities and syntax, let s look at some slightly more complicated examples. 8.6.1 Parameterization Our previous example employed the simple JDOQL filter name == “Ogilvie Partners” Now we ll add parameterization. The test will no longer be against the string literal Ogilvie Partners, but against an incoming string parameter. Extent partnerExt = pm.getExtent(BusinessPartner.class, true); String filter = “name == searchName”; Query q = pm.newQuery(partnerExt, filter); q.declareParameters(”String searchName”); q.setOrdering(”name ascending, partnerId descending”); Collection c = (Collection) q.execute(”Ogilvie Partners”); The filter has been changed, so that name is compared with searchName. In turn, searchName is declared as a parameter to the query. Finally a String is passed to the query execution method. It is permissible for the query parameter to have the same name as a persistent field. In such cases, the keyword “this” is used to differentiate between the two, as per the example below. Extent partnerExt = pm.getExtent(BusinessPartner.class, true); String filter = “this.name == name”; Query q = pm.newQuery(partnerExt, filter); q.declareParameters(”String name”); q.setOrdering(”name ascending, partnerId descending”); Collection c = (Collection) q.execute(”Ogilvie Partners”); There is only one parameter in this case. If there had been multiple parameters, the parameter declaration would be comma-delimited. Parameters would be bound to values at runtime according to the sequences with which parameter declarations and values were given. In this regard they are positional parameters. Here is an example with multiple 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, partierId descending”); Collection c; c = (Collection) q.execute(”Ogilvie Partners”, “Milton Keynes”);
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

Comments are closed.