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.

Sub-Queries<br />

We can get the average salary of each department using a single query method:<br />

SELECT e.DEPARTMENT, AVG(e.SALARY) FROM EMPLOYEE e GROUP BY e.DEPARTMENT<br />

The results are:<br />

DEPARTMENT<br />

AVG(SALARY)<br />

Coffee Room 950<br />

Mail Room 1100<br />

The Closet with the Light Out 850<br />

Sub-Queries<br />

Dynamic Queries<br />

Sub-queries are permitted as deep as the database implementation being queried<br />

allows. For example, you could use the following sub-query (in bold) specified in ejbjar.xml.<br />

Note that the sub-query includes ORDER BY as well, and the results are to be<br />

returned in descending (DESC) order.<br />

<br />

<br />

findApStatisticsWithGreaterThanAverageValue<br />

<br />

<br />

SELECT Object(s1) FROM ApStatistics s1 WHERE s1.averageValue ><br />

SELECT AVG(s2.averageValue) FROM ApStatistics s2 ORDER BY s1.averageValue<br />

DESC<br />

<br />

See your database implementation documentation for details on the appropriate use of<br />

sub-queries.<br />

There are situations where you may need to search dynamically for data, based on<br />

variable criteria. Unfortunately EJB-QL queries do not support this scenario. Since<br />

EJB-QL queries are specified in the deployment descriptor, any changes to the queries<br />

require re-deployment of the bean. The <strong>Borland</strong> Enterprise Server offers a Dynamic<br />

Query feature which allows you to construct and execute EJB-QL queries dynamically<br />

and programmatically in the bean code.<br />

Dynamic queries offer these benefits:<br />

■<br />

allow you to create and execute new queries without having to update and deploy<br />

an EJB.<br />

■<br />

reduce the size of the EJB's deployment descriptor file because finder queries can<br />

be dynamically created instead of statically defined in the deployment descriptors.<br />

Dynamic queries don't need to be added to the deployment descriptor. They are<br />

declared in the bean class for dynamic ejbSelects, or in the local or remote home<br />

interfaces for dynamic finders.<br />

A finder method for a dynamic query is:<br />

public java.util.Collection findDynamic(java.lang.String ejbql, Class[]<br />

types, Object[] args)<br />

throws javax.ejb.FinderException<br />

public java.util.Collection findDynamic(java.lang.String ejbql, Class[]<br />

types, Object[] args, java.lang.String sql)<br />

throws javax.ejb.FinderException<br />

Chapter 17: EJB-QL and Data Access Support 157

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

Saved successfully!

Ooh no, something went wrong!