EJB QL Examples
EJB QL is expressed in terms of the abstract persistence schema of an entity bean: its abstract schema name, CMP fields, and CMR fields. EJB QL uses the abstract schema names to identify beans, the CMP fields to specify values, and the CMR fields to navigate across relationships.
To discuss EJB QL, we will make use of the relationships among the Customer, Address, CreditCard, Cruise, Ship, Reservation, and Cabin EJBs defined in Chapter 7. Figure 8-1 is a class diagram that shows the direction and cardinality (multiplicity) of the relationships among these beans.
Figure 8-1. Titan Cruises class diagram
Simple Queries
The simplest EJB QL statement has no
WHERE
clause and only one abstract schema type.
For example, you could define a query method to select all Customer
beans:
SELECT OBJECT( c ) FROM Customer AS c
The FROM
clause determines which entity bean
types will be included in the select statement (i.e., provides the
scope
of the select). In this case, the FROM
clause
declares the type to be Customer
, which is the
abstract schema name of the Customer EJB. The AS
c
part of the clause assigns c
as the identifier of the Customer EJB. This is similar to SQL, which allows an identifier to be associated with a table. Identifiers can be any length and follow the same rules that are applied to field names in the Java programming language. However, identifiers cannot ...
Get Enterprise JavaBeans, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.