Skip Headers

# Redirect Queries

Previous

Next

To accommodate complex query logic, you can implement a redirect query: a named query that delegates query execution control to your application. For more information, see "Named Queries".

Redirect queries lets you define the query implementation in code as a static method. When you invoke the query, the call redirects to the specified static method. Redirect queries accept any arbitrary parameters passed into them packaged in a `Vector`.

Although most TopLink queries search for objects directly, a redirect query generally invokes a method that exists on another class and waits for the results. Redirect queries let you build and use complex operations, including operations that might not otherwise be possible within the query framework.

By delegating query invocation to a method you provide, redirect queries let you dynamically make decisions about how a query should be executed based on argument values.

Using a redirect query, you can do the following:

- Dynamically configure the query options based on the arguments (for example, ordering and query optimization)

- Dynamically define the selection criteria based on the arguments

- Pass query-by-example objects or expressions as the arguments

- Post-process the query results

- Perform multiple queries or special operations

If you execute the query on a `UnitOfWork`, the results register with that instance of `UnitOfWork`, so any objects you attempt to retrieve with the invoke method must come from the `Session` cache.

To create a redirect query, you implement the `QueryRedirector` interface and set your implementation on a named query.

Oracle recommends that you take advantage of the `MethodBasedQueryRedirector`, an instance of `QueryRedirector` that TopLink provides. It takes the name of a static method and the `Class` in which it is defined as parameters. When you set a `MethodBasedQueryRedirector` on a named query, whenever `invokeQuery` method is called on this instance, TopLink uses reflection to invoke your static method instead.

The advantages of using a `MethodBasedQueryRedirector` are as follows:

- You can specify the static method and its `Class` dynamically.

- The class that provides the static method does not need to implement `QueryRedirector`.

- Your static method can have any name.

- You can restrict the parameters to your static method to only a `Session` and a `Vector` of arguments.

For more information, see "Using Redirect Queries".