Skip Headers

# Expression Method getParameter

Previous

Next

The `Expression` method `getParameter` returns an expression that becomes a parameter in the query. This lets you create a query that includes user input in the search criteria. The parameter must be either the fully qualified name of the field from a descriptor's row, or a generic name for the argument.

Parameters you construct this way are global to the current query, so you can send this message to any expression object.

Example: Using a Parameterized Expression in a Custom Query illustrates how to use a custom query to find an employee by first name.

Using a Parameterized Expression in a Custom Query

    Expression firstNameExpression;

    ReadObjectQuery query = new ReadObjectQuery(Employee.class);
    ExpressionBuilder emp = query.getExpressionBuilder();
    firstNameExpression = emp.get("firstName").equal(emp.getParameter("firstName"));
    query.setSelectionCriteria(firstNameExpression);
    query.addArgument("firstName");
    Vector v = new Vector();
    v.addElement("Sarah");
    Employee e = (Employee) session.executeQuery(query, v);

Example: Using Nested Parameterized Expressions illustrates how to use a custom query to find all employees that live in the same city as a given employee.

Using Nested Parameterized Expressions

    Expression addressExpression;
    ReadObjectQuery query = new ReadObjectQuery(Employee.class);
    ExpressionBuilder emp = query.getExpressionBuilder();
    addressExpression =
        emp.get("address").get("city").equal(
        emp.getParameter("employee").get("address").get("city"));
    query.setName("findByCity");
    query.setSelectionCriteria(addressExpression);
    query.addArgument("employee");
    Vector v = new Vector();
    v.addElement(employee);
    Employee e = (Employee) session.executeQuery(query, v);

Example: Using a Parameterized Expression in a Mapping illustrates how to obtain a simple one-to-many mapping from class `PolicyHolder` to `Policy` using a nondefault selection criteria. The SSN field of the POLICY table is a foreign key to the SSN field of the HOLDER table.

Using a Parameterized Expression in a Mapping

    OneToManyMapping mapping = new OneToManyMapping();
    mapping.setAttributeName("policies");
    mapping.setGetMethodName("getPolicies");
    mapping.setSetMethodName("setPolicies");
    mapping.setReferenceClass(Policy.class);

    // Build a custom expression here rather than using the defaults
    ExpressionBuilder policy = new ExpressionBuilder();
    mapping.setSelectionCriteria(policy.getField("POLICY.SSN")).equal(policy.
      getParameter("HOLDER.SSN")));