Class MethodBaseQueryRedirector
- java.lang.Object
-
- org.eclipse.persistence.queries.MethodBaseQueryRedirector
-
- All Implemented Interfaces:
Serializable,QueryRedirector
public class MethodBaseQueryRedirector extends Object implements QueryRedirector
Purpose:
Allows a class to be aQueryRedirectorwithout implementingQueryRedirector.Description:
Normally to define a Redirector a Class must implementQueryRedirectorand the requiredQueryRedirector.invokeQuery(DatabaseQuery, Record, Session).To maintain transparency it is possible to instead only define a static method that takes the same arguments as
An instance ofinvokeQuery.MethodBaseQueryRedirectorcan be constructed, taking the name of that static method and theClassin which it is defined as parameters.Whenever
Advantages:invokeQueryis called on this instance reflection will automatically be used to invoke the custom method instead.- The Redirector class and method name can be specified dynamically.
- The class containing the
invokeQuerymethod does not need to implementQueryRedirector. - The
invokeQuerymethod can have any name. - The
invokeQuerymethod can alternatively be defined to accept onlySession sessionandVector argumentsas parameters.
- An extra step is added as the real
invokeQuerymethod is called dynamically.
Example:
// First create a named query, define a redirector for it, and add the query // to the query manager. ReadObjectQuery query = new ReadObjectQuery(Employee.class); query.setName("findEmployeeByAnEmployee"); query.addArgument("employee"); MethodBaseQueryRedirector redirector = new MethodBaseQueryRedirector(QueryRedirectorTest.class, "findEmployeeByAnEmployee"); query.setRedirector(redirector); ClassDescriptor descriptor = getSession().getDescriptor(query.getReferenceClass()); descriptor.getQueryManager().addQuery(query.getName(), query); // Now execute the query by name, passing in an Employee as an argument. Vector arguments = new Vector(); arguments.addElement(employee); objectFromDatabase = getSession().executeQuery("findEmployeeByAnEmployee", Employee.class, arguments); // Note this Class does not implement QueryRedirector or method invokeQuery. public class QueryRedirectorTest { public static Object findEmployeeByAnEmployee(DatabaseQuery query, Record arguments, Session session) { ((ReadObjectQuery) query).setSelectionObject(arguments.get("employee")); return session.executeQuery(query); } }- Since:
- TOPLink/Java 3.0
- Author:
- James Sutherland
- See Also:
QueryRedirector, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Methodmethodprotected Class<?>methodClassprotected StringmethodClassNameprotected StringmethodName
-
Constructor Summary
Constructors Constructor Description MethodBaseQueryRedirector()PUBLIC: Returns a new query redirector.MethodBaseQueryRedirector(Class<?> methodClass, String methodName)PUBLIC: Returns a new query redirector based on the static method in methodClass.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MethodgetMethod()INTERNAL: Returns the static method.Class<?>getMethodClass()PUBLIC: Returns the class to execute the static method on.StringgetMethodClassName()INTERNAL: Returns the class to execute the static method on.StringgetMethodName()PUBLIC: Returns the name of the static method.protected voidinitializeMethod(DatabaseQuery query)INTERNAL: Set the method.ObjectinvokeQuery(DatabaseQuery query, DataRecord arguments, Session session)INTERNAL: Call the static method to execute the query.protected voidsetMethod(Method newMethod)INTERNAL: Sets the static method.voidsetMethodClass(Class<?> newMethodClass)PUBLIC: Sets the class to execute the static method on.voidsetMethodClassName(String newMethodClassName)INTERNAL: Sets the class to execute the static method on.voidsetMethodName(String newMethodName)PUBLIC: Sets the name of the static method.
-
-
-
Method Detail
-
getMethod
protected Method getMethod()
INTERNAL: Returns the static method.
-
getMethodClass
public Class<?> getMethodClass()
PUBLIC: Returns the class to execute the static method on.
-
getMethodClassName
public String getMethodClassName()
INTERNAL: Returns the class to execute the static method on.
-
getMethodName
public String getMethodName()
PUBLIC: Returns the name of the static method. This method must be public, static and have argument of DatabaseQuery, Vector, Session.- See Also:
setMethodName(java.lang.String)
-
initializeMethod
protected void initializeMethod(DatabaseQuery query) throws QueryException
INTERNAL: Set the method.- Throws:
QueryException
-
invokeQuery
public Object invokeQuery(DatabaseQuery query, DataRecord arguments, Session session)
INTERNAL: Call the static method to execute the query.- Specified by:
invokeQueryin interfaceQueryRedirector
-
setMethod
protected void setMethod(Method newMethod)
INTERNAL: Sets the static method.
-
setMethodClass
public void setMethodClass(Class<?> newMethodClass)
PUBLIC: Sets the class to execute the static method on.
-
setMethodClassName
public void setMethodClassName(String newMethodClassName)
INTERNAL: Sets the class to execute the static method on.
-
setMethodName
public void setMethodName(String newMethodName)
PUBLIC: Sets the name of the static method.This method must be public, static and have arguments of DatabaseQuery, Record, and Session.
The DatabaseQuery argument is the query that is currently being executed.
The Record will contain the Argument names added to the Query through addArgument(Sting) or, in the case of an Object query, the object attribute field names. These names will reference the argument values passed into the query, or in the case of an Object Query the values from the object.
The session argument is the session that the query is currently being executed on.
Alternatively the method can take only
(Session session, Vector arguments)as parameters.
-
-