EJB 2.0 provides a query syntax called EJB QL for both finder and select methods of CMP entity beans.
Finder methods obtain one or more entity bean instances from a database, and are defined in the home interface. Select methods are defined on the abstract bean class and can return entity beans (any entity bean type defined in the EJB JAR file) or CMP field values.
The <query> element is used to define the query for the finder method in the deployment descriptor, for every finder method except findByPrimaryKey(key). Queries specified in the deployment descriptor are compiled into SQL during deployment. The query statement is contained in the <ejb-ql> element of the <query> element:
<query> <query-method> <method-name></method-name> <method-params> <method-param></method-param> </method-params> </query-method> <result-type-mapping></result-type-mapping> <ejb-ql></ejb-ql> </query>
Query Language Definition
An EJB query is a string that contains the following elements:
The SELECT clause is optional in order to maintain compatibility with WebSphere Application Server Version 4.
The query can also contain input parameters that correspond to the arguments of the finder method.
Comparison of EJB 2.0 specification and WebSphere Query Language
WebSphere's EJB query language is compliant with the EJB QL defined in Sun's EJB 2.0 specification and has additional capabilities as listed in the table below.
Item | EJB 2.0 specification | WebSphere Query | WebSphere Enterprise (Dynamic) Query |
---|---|---|---|
Bean methods | no | no | yes |
Calendar comparisons | yes | yes | yes |
Delimited identifiers | no | yes | yes |
Dependent value attributes | no | yes | yes |
Dependent value methods | no | no | yes |
Dynamic Query APIs | no | no | yes |
EXISTS predicate | no | yes | yes |
Inheritance | no | yes | yes |
Multiple element select clauses | no | no | yes |
Order by | no | yes | yes |
Scalar functions | yes * | yes | yes |
Select clause | required | optional | optional |
SQL Date/time expressions | no | yes | yes |
String comparisons | = and <> only | = <> > < | = <> > < |
Subqueries, aggregations, group by, and having clauses | no | yes | yes |
* EJB 2.0 defines the following scalar functions: abs, sqrt, concat, length, locate and substring. WebSphere query supports additional scalar functions. Please refer to the related topic: EJB query: Scalar functions.
Where to find additional information about the EJB query language
Detailed information on how to structure EJB queries is found in Chapter 11 of the EJB 2.0 specification available at java.sun.com/products/ejb/docs.html. However, the WebSphere documentation contains Sun's information along with the Websphere extensions.