Skip Headers

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

Previous | Next |

# Using Redirect Queries

A redirect query is a named query that delegates query execution control to your application. redirect queried allow you to define the query implementation in code as a static method.

To perform complex operations, you can combine query redirectors with the TopLink query framework.

## Creating a Redirect Query

To perform complex operations, you can combine query redirectors with the TopLink query framework. To create a redirector, implement the `oracle.toplink.queryframework.QueryRedirector` interface. The query mechanism executes the `Object invokeQuery(DatabaseQuery query, DatabaseRow arguments, Session session)` method and waits for the results.

TopLink provides one preimplemented redirector, the `MethodBasedQueryRedirector` method. To use this redirector, create a static `invoke method` on a class, and use the `setMethodName(String)` call to specify the method to invoke.

Example 99-1 Redirect Query

    ReadObjectQuery query = new ReadObjectQuery(Employee.class);
    query.setName("findEmployeeByAnEmployee");
    query.addArgument("employee");

    MethodBaseQueryRedirector redirector = new
         MethodBaseQueryRedirector(QueryRedirectorTest.class, "findEmployeeByAnEmployee");
    query.setRedirector(redirector);
    Descriptor descriptor = getSession().getDescriptor(query.getReferenceClass());
    descriptor.getQueryManager().addQuery(query.getName(), query);

    Vector arguments = new Vector();
    arguments.addElement(employee);
    objectFromDatabase = getSession().executeQuery(query,arguments);

    public class QueryRedirectorTest {
        public static Object findEmployeeByAnEmployee(
            DatabaseQuery query,
            oracle.toplink.publicinterface.DatabaseRow arguments,
            oracle.toplink.sessions.Session
            session) {
                ((ReadObjectQuery) query).setSelectionObject(arguments.get("employee"));
                return session.executeQuery(query);
        }
    }