Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc

EJB QL Definition

Enterprise JavaBeans 2.0, Public Draft EJB QL: EJB Query Language for Container

Figure 36 The abstract schemas of OrderEJB and ProductEJB are in different ejb-jar files.

ejb-jar file 1

 

ejb-jar file 2

OrderBean

 

 

OrderBean

 

 

1

m

 

 

 

 

LineItem

 

 

Product

ProductBean

Shipping

BillingAddress

 

Address

 

 

10.2.2 Naming

EJB QL uses the following naming scheme for identifying entity beans and their abstract schemas:

It designates an entity bean that has an abstract schema within the same ejb-jar file by its ejb-name.

It designates a dependent class by its dependent-name.

It designates a remote entity bean participating in a relationship by the remote-ejb-name assigned to it by the ejb-entity-ref deployment descriptor element.

For a local entity bean, EJB QL also utilizes the abstract schema name of the entity bean to refer to an abstract schema type[16]. The abstract schema type of the entity bean corresponds to the type of the

entity bean’s class. The name of the abstract schema type of each entity bean must also be defined in the deployment descriptor. For example, the abstract schema name of the bean implemented by the entity bean class com.acme.ecommerce.OrderBean would typically be designated by the name OrderBean in the deployment descriptor.

The naming of entity beans (local or remote) is part of the development process. The Bean Provider assigns unique names to entity beans and to dependent object classes so that they can be used within the queries. These unique names are scoped within the ejb-jar file. The EJB 2.0 specification introduces this naming approach, which differs slightly from the naming approach of EJB 1.1. As a result, the application assembler must not change the names of entity beans, their abstract schemas, and their dependent objects when resolving ejb-links. The linking convention for resolving ejb-names is discussed in Section 19.3.2.

[16]Although it is possible to uniquely designate the abstract schema type of a local entity bean from the ejb-name, it is easier to use a unique name that designates an abstract schema type rather than introducing a notation to designate the abstract schema type based on an ejb-name by introducing a special operator.

199

5/31/00

Sun Microsystems Inc.

EJB QL: EJB Query Language for Container Managed Persistence Finder MethodsEnterprise JavaBeans 2.0, Public Draft

EJB QL

The rest of this chapter uses sample queries based on the extended examples in case(1) and case(2) to illustrate the capabilities of EJB QL. The sample queries use ProductEJB in different ways to illustrate the navigational differences between local beans and remote beans. The following sections cover in detail how to evaluate and use local and remote types within EJB QL.

10.2.3 Navigation Declarations and the FROM Clause

The FROM clause in EJB QL describes the scope of the finder query and declares correlation variables. Unlike in SQL, the purpose of the FROM clause in EJB QL is only to declare correlation variables.

The correlation variables designate instances of a particular type in a finder query. The FROM clause can contain multiple correlation variable declarations separated by a comma (,).

From_Clause ::= FROM Correlation_variable_declaration {, Correlation_variable_declaration} Correlation_variable_declaration ::= Collection_member_declaration | Reference_declaration Collection_member_declaration ::= identifier IN Collection_valued_path_expression |

identifier IN Collection_valued_reference_expression Reference_declaration ::= identifier FOR [SchemaName | Single_valued_path_expression |

Single_valued_reference_expression]

A correlation variable designates a single value reference and is declared in one of two ways:

A collection member variable declared by using a collection valued path expression.

A reference declared by using a single valued path expression or an abstract schema name.

A path expression represents a navigation involving the cmp-fields and cmr-fields in the abstract schema of a local entity bean or a dependent object class. Because a path expression can be based on another path expression, the navigation can use the cmp-fields and cmr-fields of related entity beans and dependent object classes. The navigation operator in a path expression is reserved and is represented by a period (.). This operator can be used only for navigation within the abstract schema of a local entity bean or dependent object class. Section 10.2.4.5 covers path expressions.

A correlation variable, when declared by a Collection_member_declaration, ranges over values of a collection. It is declared using a special operator, the reserved identifier IN, followed by a path expression. The path expression in the declaration evaluates to a collection type specified as a result of navigation over collection-valued cmr-fields in an entity bean’s or a dependent object class’s abstract schema. Collection valued cmr-fields are designated by one-to-many or many-to-many relationships.

For example, a FROM clause for the OrderBean finder might have the following correlation variable declaration clause:

5/31/00

200

Sun Microsystems Inc

EJB QL Definition

Enterprise JavaBeans 2.0, Public Draft EJB QL: EJB Query Language for Container

l in lineitems

In this example, lineitems is the name of a cmr-field whose value is a collection of LineItems. The correlation variable l designates a member of the collection, a single LineItem value.

Correlation variables must designate members of collection-valued cmr-fields. For example, the correlation variable declaration P in lineitems.product is illegal because lineitems evaluates to a collection and path expressions cannot be further defined using collection valued path expressions.

Correlation variables can be declared for navigations involving cmp-fields or single valued cmr-fields, which designate a one-to-one relationship. Such a correlation variable is declared using the special operator FOR followed by a single valued path expression.

A correlation variable can also be declared to designate an abstract schema value or a remote object designating an entity object. For example, the declaration expression O FOR OrderBean designates correlation variable O as a reference to a value of type OrderBean; P FOR l=>Product designates a correlation variable P as a reference to a remote value of the type Product.

A correlation variable declaration can use the results of a previous correlation variable declaration. Thus, a correlation variable can be declared using a path expression which contains a correlation variable. The correlation variable declarations are evaluated from left to right in the FROM clause.

For example, the following FROM clause contains three correlation variable declaration clauses, and a correlation variable declared in one clause is used in the other clause. The navigation clauses declare the variables O, l and p respectively. The correlation variable l utilizes the results of the navigation on O, and p utilizes the results of the navigation on l.

FROM O FOR OrderBean, l IN O.lineitems, p FOR l.product

The correlation variable l has the type LineItem and p evaluates to a value of type ProductBean, which is the abstract schema type for the ProductBean entity bean. The correlation variable O evaluates to the abstract schema type of OrderBean, referenced by the name OrderBean.

Correlation variables are existentially quantified in an EJB QL query and they must be legal identifiers in EJB QL.

A FROM clause can consist only of correlation variable declarations that are based on valid path expressions originating from the entity bean’s abstract schema. A FROM clause cannot specify arbitrary “joins”; it can only designate allowable declarations based on the abstract schema of the entity bean and navigations allowed from the abstract schema. For example, the following FROM clause for a finder query of the OrderEJB entity bean is illegal although syntactically correct:

FROM l IN LineItems, P FOR ProductBean

This query is illegal because ProductBean is neither a cmp-field nor a cmr-field in the abstract schema of the OrderEJB entity bean. The query refers to the abstract schema type of the ProductEJB entity bean, which is not obtained via navigation. All correlation variables depend on expressions that are based on navigation over the entity bean’s abstract schema. However, the following FROM clause is legal since it is based on navigability. The correlation variable l can be used in the second declaration clause since the variable declarations are evaluated in order from left to right.

201

5/31/00