When a Parameterized Query is used, each numbered placeholder is substituted with the value of an entire object at runtime. However, it is sometimes more useful to substitute in the value of one of the object’s fields, rather than the entire value of the object. A Parameterized Field Query specifies this using the syntax ?{number}.{fieldName}. For example:

name = ?0.name AND age = ?0.age

In this example, only one object is passed into the query at runtime. However, this object is expected to have two public member variables called name and age. The query will extract the values of these member variables from the object and substitute those values for the ?0.name and ?0.age parameters. Note that the fields must be public member variables of the object that is passed in, not JavaBean properties. For example, the following object could be passed in to the query:

public class QuerySpecifier {
  public String name;
  public int age;
}

Parameterized Field Queries are used most often for entity EJBs, which allow primary key classes to contain multiple fields. In this case, only one object will be passed to the query (the primary key), but if the primary key spans multiple database fields, then the primary key object will contain the values of those fields in its public member variables.

 
loading table of contents...