Section C.1. Basic Structure of EJB QL Queries


C.1. Basic Structure of EJB QL Queries

EJB QL queries contain the following four clauses:


SELECT clause (required)

Indicates what beans or CMP fields are being returned


FROM clause (required)

Indicates what subset of the abstract schema is being queried


WHERE clause (optional)

Indicates optional conditional logic to use in selecting the results


ORDER BY clause (optional) (added in EJB 2.1)

Indicates how the results should be sorted when returned

So an EJB QL query takes the following high-level form:

 SELECT <select clause> FROM <from clause>    [WHERE <where clause>] [ORDER BY <sort clause>] 

Queries are declared in an EJB deployment descriptor using the following format:

 ... <entity>   ...   <query>     ...     <ejb-ql>       <[!CDATA[SELECT p FROM Person p WHERE p.name IS NOT NULL                ORDER BY p.lastName, p.firstName]]>     </ejb-ql>     ...   </query> ... </entity> ... 

The complete syntax for EJB deployment descriptors can be found in Appendix A.

In a query, the FROM clause specifies the domain of the abstract schema that is being queried. This is done by declaring variables that are bound to different elements of the abstract schema. The SELECT clause is used to specify what elements of the schema you want returned as the result set, as a subset of the domain defined by the FROM clause. The optional WHERE clause is used to specify the conditions used to match the elements to return, and the optional ORDER BY clause is used to order the result set by one or more data fields contained in the results.

Since the FROM clause defines the variables involved in the query, we'll start there, then describe the SELECT clause. After these required clauses, we discuss the optional WHERE clause and its conditional expressions and the optional ORDER BY clause for sorting the returned results.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net