12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Aggregate Functions in EJB-QL<br />

Selecting a ResultSet<br />

When more than one cmp-field is to be returned by a single query method, the return<br />

type must be of type ResultSet. This allows you to select multiple cmp-fields from the<br />

same or multiple EJBs in the same query method. You then write code to extract the<br />

desired data from the ResultSet. This feature is a <strong>Borland</strong> extension of the CMP 2.x<br />

specification.<br />

Aggregate Functions in EJB-QL<br />

Aggregate functions are MIN, MAX, SUM, AVG, and COUNT. For the aggregate<br />

functions MIN, MAX, SUM, and AVG, the path expression that forms the argument for<br />

the function must terminate in a cmp-field. Also, database queries for MAX, MIN, SUM,<br />

and AVG will return a null value if there are no rows corresponding to the argument to<br />

the aggregate function. If the return type is an object-type, then null is returned. If the<br />

return type is a primitive type, then the container will throw a ObjectNotFoundException<br />

(a sub-class of FinderException) if there is no value in the query result.<br />

The path expression to the COUNT functions may terminate in either a cmp-field or<br />

cmr-field, or may be an identification variable.<br />

For example, the following EJB-QL aggregate function terminates in a CMP field:<br />

<br />

<br />

ejbSelectMaxLineItemId<br />

<br />

java.lang.String<br />

<br />

<br />

Local<br />

SELECT MAX(l.lineItemId) FROM Account AS a, IN (a.accountLineItem) l<br />

WHERE l.accountId=?1<br />

<br />

The following restrictions must be observed for aggregate functions:<br />

■<br />

■<br />

■<br />

Arguments to the SUM and AVG functions must be numeric (Integer, Byte, Long,<br />

Short, Double, Float, and BigDecimal).<br />

Arguments to the MAX and MIN functions must correspond to orderable cmp-field<br />

types (numeric, string, character, and dates).<br />

The path expression that forms the argument for the COUNT function can terminate<br />

in either a cmp-field or a cmr-field. Application performance is greatly enhanced<br />

when the COUNT function is used to determine the size of a collection of cmr-fields.<br />

Data Type Returns for Aggregate Functions<br />

The following table shows the data types that can be arguments for the various<br />

aggregate functions in EJB-QL selecting a single object, and what data types will be<br />

returned.<br />

An aggregate function that selects multiple objects returns a collection of the<br />

wrappered Java data type that is returned.<br />

Table 17.1<br />

Aggregate functions return types<br />

Aggregate Function Argument data type Expected return type<br />

MIN, MAX, SUM java.lang.Integer java.lang.Integer<br />

AVG java.lang.Integer java.lang.Double<br />

154 BES Developer’s Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!