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

Sun Microsystems Inc

Object interaction diagrams

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

Chapter 10 EJB QL: EJB Query Language for Container Managed Persistence Finder Methods

The EJB QL query language defines finder methods for entity beans with container managed persistence. EJB QL defines finder methods so that they are portable across containers and persistence managers. EJB QL is a declarative, SQL-like language intended to be compiled to the target language of the persistent data store used by a persistence manager. It is based on a subset of SQL92 which is enhanced by path expressions that allow navigation over the relationships defined for entity beans and their dependent object classes.

193

5/31/00

Sun Microsystems Inc.

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

10.1 Overview

The Enterprise JavaBeans query language, EJB QL, is used to define finder queries for entity beans with container managed persistence. EJB QL lets the Bean Provider specify finder methods in a portable way. It is a specification language that can be compiled to a target language, such as SQL, of a persistent store used by a persistence manager. This allows the responsibility for the execution of finder queries to be shifted to the native language facilities provided for the persistent store (e.g., RDBMS), instead of requiring finder queries to be executed directly on the persistent manager’s representation of the entity beans’ state. As a result, finder methods are both portable and optimizable.

The Enterprise JavaBeans query language uses the abstract schema of entity beans, their dependent object classes, and their relationships for its data model. It defines operators and expressions based on the data model.

A finder query for an entity bean is a String which may have a SELECT clause, a FROM clause, and a WHERE clause.

The Bean Provider uses EJB QL to write finder queries based on the relationships defined in the deployment descriptor. EJB QL depends on navigation and selection based on the cmp-field and cmr-fields of the abstract schemas of related entity beans and their dependent objects. The Bean Provider can navigate from an entity bean to its dependent objects or to other related beans by using the names of the cmr-fields in a finder query.

The Bean Provider can navigate to both locally defined entity beans and remote entity beans (entity beans with bean managed persistence, entity beans using EJB 1.1 container managed persistence, and beans whose abstract schemas are defined in a different ejb-jar file). EJB QL allows the Bean Provider to use the abstract schemas of related entity beans in a query if these abstract schemas are available in the same ejb-jar file. Although the abstract schemas of remote entity beans are not available to the Bean Provider, it is still possible to use EJB QL to navigate to such remote entity beans. Special expressions in the language allow the Bean Provider to include finder methods on remote entity beans in EJB QL queries.

EJB QL queries can be specified in two different ways:

as queries for selecting existing entity objects which are described by finder methods in the home interface. This use of EJB QL allows the results of a finder query to be usable by the clients of an entity bean.

as queries for selecting values derived from an entity bean’s or its dependent objects’ abstract schemas. This use of EJB QL allows the Bean Provider to find values related to the state of an entity bean without exposing the results to the client.

Consider an entity bean OrderEJB and its entity bean class, which has a one-to-many relationship with a LineItem dependent object class. EJB QL allows the Bean Provider to specify finder queries for OrderEJB by navigating over the cmr-fields of the entity bean class OrderBean and its related dependent object class LineItem. For example, a finder method query to find all orders with pending line items might be written as:

5/31/00

194

Sun Microsystems Inc

EJB QL Definition

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

FROM l in lineitems WHERE l.shipped IS FALSE

This query navigates over the cmr-field lineitems of OrderBean to find line items, and uses the cmp-field shipped of the dependent object class LineItem to select those instances of OrderBean that have at least one line item that has not yet shipped. This example does not use a SELECT clause because the query is defined for a finder method on the entity bean’s home interface and must therefore always return entity objects of type OrderEJB.

10.2 EJB QL Definition

EJB QL uses a SQL-like syntax to select entity objects based on their abstract schemas and relationships[15]. The path expressions of EJB QL allow the Bean Provider to navigate over relationships defined by the cmr-fields of the abstract schemas of entity beans and dependent object classes.

This section provides the full definition of the language.

An EJB QL query is a String consisting of the following three clauses:

an optional SELECT clause, which indicates the types of values to be selected.

an optional FROM clause, which provides navigation declarations that designate the domain to which the conditional expression specified in the WHERE clause applies.

an optional WHERE clause whose syntax is based on a subset of the SQL92 conditional expression syntax.

In BNF syntax, an EJB QL query is defined as:

EJB QL :: = {Select_clause} {From_clause} {Where_clause}

EJB QL is a typed expression language. Every expression in EJB QL has a type. The type of the expression is derived from the structure of the expression; the abstract schema declarations and the object types to which the cmp-fields and cmr-fields evaluate; the types of literals; and the types of the correlation variables.

It is possible to parse and validate EJB QL queries before entity beans are deployed because EJB QL is based on the abstract schemas of entity beans and dependent object classes. The allowable types in EJB QL are the abstract schema types of entity beans and dependent objects, the defined types of cmp-fields and cmr-fields, and the types of remote objects. The abstract schema type of an entity bean is derived from its bean class, which defines the cmp-fields and cmr-fields.

[15]Arbitrary constructors which allow developers to create new objects, flatten structures, map dependent object class values t o dependent value class values, etc., are not within the current design goals of EJB QL.

195

5/31/00