Skip Headers

# Specifying Read Ordering

Previous

Next

Ordering is a common `DatabaseQuery` option. Use the Order tab in TopLink Workbench to order the collection of objects returned from a `ReadAllQuery`, or the `addOrdering`, `addAscendingOrdering`, or `addDescendingOrdering` methods in Java code. You can apply order based on attribute names or query keys and expressions.

A Query with Simple Ordering

    // Retrieves objects ordered by last name then first name in ascending order
    ReadAllQuery query = new ReadAllQuery(Employee.class);
    query.addAscendingOrdering ("lastName");
    query.addAscendingOrdering ("firstName");
    Vector employees = (Vector) session.executeQuery(query);

A Query with Complex Ordering

    /* Retrieves objects ordered by street address, descending case-insensitive order of cities, and manager's last name */
    ReadAllQuery query = new ReadAllQuery(Employee.class);
    ExpressionBuilder emp = query.getExpressionBuilder();
    query.addOrdering (emp.getAllowingNull("address").get("street"));
    query.addOrdering(
        emp.getAllowingNull("address").get("city").toUpperCase().descending());
    query.addOrdering(emp.getAllowingNull("manager").get("lastName"));
    Vector employees = (Vector) session.executeQuery(query);

Note the use of `getAllowingNull`, which creates an outer join for the address and manager relationships. This ensures that employees without an address or manager still appear in the list.

For more information about configuring read ordering, see "Configuring Read All Query Order".