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

Sun Microsystems Inc.

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

10.2.8 Restrictions

Date and time values should use the standard Java long millisecond value. A date or time literal included in an EJB QL query should be an integer literal for a millisecond value. The standard way to produce millisecond values is to use java.util.Calendar.

Although SQL supports fixed decimal comparison in arithmetic expressions, EJB QL does not. For this reason EJB QL restricts exact numeric literals to those without a decimal (and numerics with a decimal as an alternate representation for approximate numeric values).

EJB QL does not support SQL comments.

10.3 Examples

The following examples illustrate the syntax and semantics of EJB QL. These examples are based on the example of Section 10.2.1.1.

10.3.1 Simple Queries

Find all orders that need to be shipped to California:

WHERE shipping_address.state = ‘CA’

10.3.2 Queries with Dependent Classes

Find all pending orders:

FROM l in lineitems

WHERE l.shipped IS FALSE

Find all orders with a shipping address that differs from its billing address.

WHERE

NOT (shipping_address.state = billing_address.state AND shipping_address.city = billing_address.city AND shipping_address.street = billing_address.street)

If the Bean Provider uses a single class Address in two different relationships for both the shipping address and the billing address, the above expression can be simplified based on the equality rules defined in Section 10.2.7. The above query can therefore be written:

WHERE shipping_address <> billing_address

Note that in this example, the abstract schema of Address does not have any cmr-fields. As noted in Section 10.2.7, the Bean Provider is discouraged from writing such queries for dependent objects with cmr-fields because they are potentially very expensive.

5/31/00

212

Sun Microsystems Inc

Examples

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

10.3.3 Queries that refer to Other Entity Beans

Let us consider the following query: “Find all orders for a book titled ‘Programming Enterprise JavaBeans: Component-Based Development for the J2EE Platform’”

The following example illustrates the situation when OrderEJB and ProductEJB are both deployed within the same ejb-jar file. The Bean Provider can use the abstract schemas of both entity beans and their dependent objects. This is covered as Case (1) in 10.2.1.1.

FROM l IN lineitems, p FOR l.product, WHERE p.type = ‘book’ AND

p.name = ‘Programming Enterprise JavaBeans: Component-Based Development for the J2EE Platform’

The following example utilizes a finder query defined as a finder method of ProductEJB. This query finds a book by its name and its author. The ProductEJB bean may or may not be co-located in the same ejb-jar file as the OrderEJB bean. Therefore, the Bean Provider uses a finder expression to locate the required entity object.

FROM l IN lineitems, p FOR l=>product

WHERE p IN (ProductEJB >> findByNameAndType(‘Enterprise

JavaBeans: Mastering Application Development’, ‘book’)

The remote interface reference expression l=>product evaluates to the remote interface type of ProductEJB, which is Product. The finder expression evaluates to a collection of values that are instances of the remote interface of the entity bean ProductEJB. The comparison operator IN evaluates the expression by testing equality based on the primary keys of the entity objects that the finder method returns.

Note that because this query uses a remote interface reference expression, it is valid for both the case where ProductEJB is co-located in the same ejb-jar file as OrderEJB and the case where it is not.

10.3.4 Queries using input parameters

A similar query may be written using the input parameters of the finder method as follows:

FROM l IN lineitems

WHERE l=>product IN (ProductEJB>>findByNameAndType(?1, ?2))

Given a product designated by an input parameter, find the orders for the given product:

FROM l in lineitems, p FOR l=>product

WHERE p = ?1

10.3.5 SELECT Queries

The following select queries illustrate how values other than entity objects are selected. Select queries are internal to the entity bean’s implementation class. They can be globally scoped or they can pertain to a particular entity bean instance.

The following EJB QL query could be used for either a globally scoped select query or for a select query based on a particular instance, depending on the select method for which it is defined:

213

5/31/00

Sun Microsystems Inc.

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

SELECT p FROM l in lineitems, p FOR l => product

When specified for an ejbSelect<name>InEntity method, this query will return all products referenced by the line items of the current entity instance.

When specified for an ejbSelect<name> method, this query will return all products that have been ordered, because the method does not scope the query to a single instance.

Depending the return type designated by the select method, the returned collection values might have duplicates.

Consider the following example:

SELECT shipping_address.city

When specified for an ejbSelect<name>InEntity method, this query will return the name of the city for the shipping address of a particular order.

When specified for an ejbSelect<name> method, this query will return the names of all the cities of the shipping addresses of all orders. Whether or not this method may return duplicate city names is determined by whether the result type of the ejbSelect<name> method is a java.util.Collection or java.util.Set.

5/31/00

214