Use the dynamic query service
Overview
Following are common reasons for using the dynamic query service rather than the regular EJB query service (which can be referred to as deployment query):
- You need to programmatically define a query at application run time, rather than at deployment.
- You need to return multiple CMP or CMR fields from a query. (Deployment queries allow only a single element to be specified in the SELECT clause.) For more information, see the Example: EJB queries article.
- You want to return a computed expression in the query.
- You want to use value object methods or bean methods in the query statement. For more information, see Path expressions.
- You want to interactively test an EJB query during development, but do not want to repeatedly deploy your application each time you update a finder or select query.
The dynamic query API is a stateless session bean; using it is similar to using any other J2EE EJB application bean. We can consult the API specifications in Reference: Generated API documentation (the section for package com.ibm.websphere.ejbquery).
The dynamic query bean has both a remote and a local interface. If you want to return remote EJB references from the query, or if the query statement contains remote methods, use the query remote interface:
remote interface = com.ibm.websphere.ejbquery.Query
remote home interface = com.ibm.websphere.ejbquery.QueryHome If you want to return local EJB references from the query, or if the query statement contains local methods, use the query local interface:local interface = com.ibm.websphere.ejbquery.QueryLocal
local home interface = com.ibm.websphere.ejbquery.QueryLocalHomeBecause it uses less application server memory, the local interface ensures better overall EJB performance than the remote.
Procedure
- Verify that the query.ear application file is installed on the application server on which your application is to run, if that server is different from the default application server created during installation of the product.
The query.ear file is located in the <WAS_HOME>/installableApps directory, where <WAS_HOME> is the location of the WebSphere Application Server. The product installation program installs the query.ear file on the default application server using a JNDI name of
(You or the system administrator can change this name.)com/ibm/websphere/ejbquery/Query- Set up authorization for the methods executeQuery(), prepareQuery(), and executePlan() in the remote and local dynamic query interfaces to control access to sensitive data. (This step is necessary only if your application requires security.)
Because one cannot control which ASN names, CMP fields, or CMR fields can be used in a dynamic EJB query, you or your system administrator must place restrictions on use of the methods. If, for example, a user is permitted to run the executeQuery method, he or she can run any valid dynamic query. In a production environment, you certainly want to restrict access to the remote query interface methods.
- Write the dynamic query as part of your application client code. We can consult the following examples as query models; they illustrate which import statements to use, and so on:
- If the CMP you want to query is on a different module, you should:
- do a remote lookup on query.ear
- map the query.ear file to the server that the queried CMP bean is installed on.
- Compile and run your client program with the file qryclient.jar in the classpath.
See also
Example: Dynamic query remote interface
Example: Dynamic query local interface
Dynamic query performance considerations
Access intent implications for dynamic query
Dynamic query API: prepareQuery() and executePlan() methods
Comparison of the dynamic and deployment EJB query services
See Also
EJB query language
Path expressions
See Also
Example: EJB queries