Java Reference
In-Depth Information
EJB Query Language
The EJB Query Language (EJB QL) is used to define query methods (for example, finder and
select methods) for CMP entity beans. EJB QL, which is based on SQL-92, can be compiled
automatically by the EJB container to a target language, such as SQL, of a database or other types of
persistent stores. This makes CMP entity beans more portable and much easier to deploy.
An EJB QL query has these three clauses:
 
SELECT
 
FROM
 
WHERE
The SELECT and FROM clauses are required, but the WHERE clause is optional. Here is the high-level
BNF syntax of an EJB QL query:
EJB QL :: = select_clause from_clause [where_clause]
The SELECT clause defines the types of the objects or values that the query returns. A return type is a
remote interface, a local interface, or a persistent field.
The FROM clause defines the scope of the query by declaring one or more identification variables, which
may be referenced in the SELECT and WHERE clauses. An identification variable represents one of the
following elements:
 
The abstract schema name of an entity bean
 
A member of a collection that is the multiple side of a one-to-many relationship
The WHERE clause is a conditional expression that restricts the objects or values retrieved by the query.
Although this is optional, most queries have a WHERE clause.
You now may have found that the syntax of EJB QL is quite similar to the syntax of SQL. They do have
a lot of similarities. However, EJB QL is not like SQL in the following aspects:
 
SQL deals with tables and rows, but EJB QL deals with objects and instances.
 
SQL has many built-in functions that EJB QL does not have.
 
The result of an EJB QL is a remote interface or a collection of remote interfaces.
For each method (except the findByPrimaryKey method) in your CMP entity bean, there must be a
<query> tag that describes this finder method. In the deployment descriptor, the EJB QL must be
wrapped in an expression that looks like this:
<!CDATA[expression]]>
expression is a valid EJB QL statement. The CDATA statement is not necessary but is recommended
because it escapes the reserved characters of XML. Since the object to be selected is obvious for these
finder methods, you do not need to put the SELECT clause into the expression. In many cases, if the
data is selected from a single entity bean object and there is no relationship that needs to be specified,
the FROM clause can be omitted too.
Look at the deployment descriptor shown in Listing 22-4 . The EJB QL for the findAllYachts method
is as follows:
<ejb-ql><![CDATA[WHERE yachtName IS NOT NULL]]></ejb-ql>
This is translated to the following SQL statement by EJB container at the deployment phase if, for
example, an Oracle database is used:
SELECT * FROM yacht
The EJB QL for the findYachtsCapacityMoreThan method is as follows:
<ejb-ql><![CDATA[FROM YachtBean cb WHERE cb.capacity > ?1]]></ejb-ql>
It may be translated into a SQL statement like this:
Search WWH ::




Custom Search