Skip Headers

# Creating a Redirect Query

Previous

Next

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, Record 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.

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("findEmployeeByAnEmployee", Employee.class, 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);
        }
    }