Skip Headers

Oracle® Application Server Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 2 (10.1.2)
Part No. B15505-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

Select Method Example

To define select methods in a CMP entity bean, do the following:

  1. Define the select method in the bean class as ejbSelect<name>.

  2. Define the select method definition in the deployment descriptor.


    Note:

    You cannot modify the query statement for an ejbSelect method in the orion-ejb-jar.xml file, as you can for finder methods.

Define the Select Method in the Bean Class

Add the select method in the bean class as an abstract method. For example, if you want to retrieve all employees whose salary falls within a range, define the ejbSelectBySalaryRange method, as follows:

public abstract Collection ejbSelectBySalaryRange(Float s1, Float s2) 
	throws FinderException;

Because the select method retrieves multiple employees, a Collection is returned. The low and high end of the salary range are input parameters, which are substituted in the EJB QL ?1 and ?2 parameters. The first input parameter is returned in ?1; the second input parameter is returned in ?2. The order of the all declared method parameters is the same as the order of the ?1, ?2, ... ?n EJB QL parameters.

Define the Select Method Definition in the Deployment Descriptor

Each select method is defined in the deployment descriptor in a <query> element. The following example shows the deployment descriptor for both the ejbSelectBySalaryRange and ejbSelectNameBySalaryRange methods:

<query>
  <description></description>
  <query-method>
    <method-name>ejbSelectBySalaryRange</method-name>
    <method-params>
      <method-param>java.lang.Float</method-param>
      <method-param>java.lang.Float</method-param>
    </method-params>
  </query-method>
  <ejb-ql>SELECT DISTINCT OBJECT(e) From Employee e 
           WHERE e.salary BETWEEN ?1 AND ?2
  </ejb-ql>
</query>
<query>
  <description></description>
  <query-method>
    <method-name>ejbSelectNameBySalaryRange</method-name>
    <method-params>
      <method-param>java.lang.Float</method-param>
      <method-param>java.lang.Float</method-param>
    </method-params>
  </query-method>
  <ejb-ql>SELECT e.empName From Employee e 
           WHERE e.salary BETWEEN ?1 AND ?2
  </ejb-ql>
</query>

Both of these methods provide two input parameters of type float. The types of these expected input parameters are defined in the <method-param> elements.

The EJB QL is defined in the <ejb-ql> element. Both methods evaluate the CMP field of salary within the EJB QL statement by the e.salary. The e represents the Employee objects; the salary represents the CMP field within that object. Separating it with a period shows the relationship between the entity bean and its CMP field.

The two input parameters designate the low and high salary ranges and are substituted in the ?1 and ?2 positions respectively.

The ejbSelectBySalaryRange method returns objects, where the DISTINCT keyword ensures that no duplicate records are returned. The ejbSelectNameBySalaryRange returns only the names of the employees, which is a String. This demonstrates one of the advantages of using select statements, in that you can return only the values of CMP fields within your objects.