Skip Headers

Oracle TopLink Developer's Guide 10g Release 3 (10.1.3) B13593-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Data Queries and Expressions

You can use expressions to retrieve data rather than objects. This is a common approach when you work with unmapped information in the database, such as foreign keys and version fields.

Expressions that query for objects generally refer to object attributes, which may in turn refer to other objects. Data expressions refer to tables and their fields. You can combine data expressions and object expressions within a single query. TopLink provides two main methods for expressions that query for data: ```getField` and ```getTable`.

## getField

The `getField` method lets you retrieve data from either an unmapped table or an unmapped field from an object. In either case, the field must be part of a table represented by that object's class; otherwise, TopLink raises an exception when you execute the query.

You can also use the `getField` method to retrieve the foreign key information for an object.

Example 97-21 illustrates how to use the data expression method (operator) `getField` with an object.

Example 97-21 Using getField with an Object

    builder.getField("[FIELD_NAME]").greaterThan("[ARGUMENT]");

## getTable

The `getTable` method returns an expression that represents an unmapped table in the database. This expression provides a context from which to retrieve an unmapped field when you use the `getField` method.

Example 97-22 illustrates how to combine both `getField` and `getTable` in the same expression.

Example 97-22 Using getTable and getField Together

    builder.getTable("[TABLE_NAME]").getField("[FIELD_NAME]").equal("[ARGUMENT]");

A common use for the `getTable` and `getField` methods is to retrieve information from a link table (or reference table) that supports a many-to-many relationship.

Example 97-23 reads a many-to-many relationship that uses a link table and also checks an additional field in the link table. This code combines an object query with a data query, using the employee's manager as the basis for the data query. It also features parameterization for the project ID.

Example 97-23 Using a Data Query Against a Link Table

    ExpressionBuilder emp = new ExpressionBuilder();
    Expression manager = emp.get("manager");
    Expression linkTable = manager.getTable("PROJ_EMP");
    Expression empToLink = emp.getField("EMPLOYEE   .EMP_ID").equal(linkTable.getField("PROJ_EMP.EMP_ID");
    Expression projToLink = linkTable.getField("PROJ_EMP
      .PROJ_ID").equal(emp.getParameter("PROJECT.PROJ_ID"));
    Expression extra = linkTable.getField("PROJ_EMP.TYPE").equal("W");
    query.setSelectionCriteria((empToLink.and(projToLink)).and(extra));