Section C.2. FROM Clause


C.2. FROM Clause

The FROM clause in an EJB QL query declares the subset of elements from the abstract schema that is the target of the query. Any elements referenced in the conditional expressions in the WHERE clause as well as the element that is specified as the return type in the SELECT clause must be included in the FROM clause declarations.

The FROM clause is made up of a list of variable declarations. Each variable is declared with a type and a unique variable name. Variable names can be any valid Java identifier. The types of the variables are taken from the abstract schema elements defined by the entity EJBs. These variables are declared as either range variables or collection member variables.

C.2.1. Range Variables

Range variables are query variables that take the type of one of the entity EJBs in your abstract schema. They are called range variables because their actual value can range over any of the actual entities present in the persistent store at the time that the query is executed (limited by the conditions in the WHERE clause, if present). For example, we used the following EJB QL query for one of the finder methods on our Profile EJB in Chapter 6:

 SELECT OBJECT(p) FROM ProfileBean p WHERE p.entriesBytes IS NULL 

In this query, the FROM clause declares a single range variable, p, and it's declared to range over the EJB type ProfileBean. ProfileBean is the abstract schema type that we declared for our Profile EJB. This is analogous to declaring a query variable in SQL that ranges over the rows in a particular table. In the case of EJB QL, rather than being satisfied by any of the rows in a table, the variable can take the value of any instance of the EJB present in the persistent store.

You can also optionally use the AS operator when declaring range variables:

 SELECT OBJECT(p) FROM ProfileBean AS p WHERE p.entriesBytes IS NULL 

The AS operator is simply an optional syntactic element and doesn't have any affect on the query itself.

You can declare multiple range variables of the same type in the FROM clause, of course:

 SELECT OBJECT(p1) FROM ProfileBean p1, ProfileBean p2 WHERE ... 

C.2.2. Collection Member Variables

A collection member variable is declared to range over the values obtained from an entity bean's container-managed relationship fields (CMR fields). These variables are declared using a path expression (prefixed by the IN operator) that specifies the particular CMR field on the bean. For example, suppose we extend our Profile EJB example to include a Person entity bean that contains a one-to-many CMR field that refers to all the Profiles owned by the Person. We made a similar extension in Chapter 6, but in that case, we defined the relationship as one-to-one. Here, to demonstrate the collection member variable, we assume it's one-to-many. We can declare a collection member variable that ranges over all Profiles owned by a Person with an EJB QL query, such as:

 SELECT DISTINCT OBJECT(prof) FROM Person p, IN(p.profiles) prof WHERE ... 

In the FROM clause, we declare a range variable p that ranges over all Person beans, then declare a collection member variable prof that ranges over the members of the profiles cmr-field on the matching Person beans. This type of the collection member variable is the abstract schema type of the collection members. In this case, the profiles CMR field contains ProfileBean EJBs, so that is the type of the prof variable.



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