Application Techniques



Writing EJB Method-Style Finder Methods

How to write method-style finder methods for CMP entity beans.

About this technique

Details

Category

Enterprise JavaBean Techniques

Description

You'll learn about:

Related reading

See the chapter on writing entity beans in the Programmer's Guide

Writing method-style finders   Top of page

Follow these rules to write a method-style finders for beans with CMP:

  1. Write an ejbFindXXX() method in the bean implementation (as you would for BMP), or subclass.

  2. In the ejbFindXXX() method, construct a SilverStream query (using the SilverStream expression language).

  3. Use the bean's context to get the bean's home object.

  4. Cast the home to AgoEJBEntityHome.

  5. Call the AgoEJBEntityHome.findByExpression() passing the in the query string.

  6. At deployment time, mark the Finder method as Method-style instead of Expression.

In this example, method-style finder in the EBCompanyHome is named findMoreComplex() and in the EBCompanyBean class is named ejbfindMoreComplex(). It includes the usual Where clause as well as an OrderBy, a Distinct and a row limit. It looks like this:

  public Collection ejbFindMoreComplex(String psWhere, 
      String psOrderBy, boolean pbDistinct, int piMaxRows) throws 
      FinderException, RemoteException 
     try 
       { 
      com.sssw.srv.ejb.AgoEJBEntityHome ejbHome = 
       (com.sssw.srv.ejb.AgoEJBEntityHome) m_context.getEJBHome(); 
      Collection collectionResult = (Collection) 
       ejbHome.findByExpression(psWhere, psOrderBy,  
        com.sssw.srv.ejb.AgoEJBEntityHome.FIND_COLLECTION,  
        pbDistinct, piMaxRows); 
      return collectionResult; 
       } 
   catch (Exception e) 
   { 
     throw new FinderException(e.toString()); 
    } 

Specifying method-style finders using the JAR Designer   Top of page

  1. In the Descriptor pane, highlight the findByMoreComplex method and open its Property Inspector.

  2. Make sure Type is set to Method.

  3. The Container locates the method of that name at runtime in the bean class or a subclass.

Specifying method-style finders in the SilverCmd DeployEJB XML file   Top of page

If you are using the SilverCmd DeployEJB command instead of the JAR Designer, you need to add entries for tabs in the <obj_finderMethodsList> subsection of the input file. The following example shows how to define the findMoreComplex() described in the previous section.

  <obj_finderMethodsList> 
    <obj_finderMethod> 
     <obj_method> 
      <name>findMoreComplex</name> 
       <methodParams type="StringArray"> 
        <el>java.lang.String</el> 
        <el>java.lang.String</el> 
        <el>java.lang.oolean</el> 
        <el>java.lang.int</el> 
             </methodParams> 
      </obj_method> 
     <finderMethodType>Method</finderMethodType> 
    </obj_finderMethod> 
  </obj_finderMethodsList> 

Notes about the XML tags

The <finderMethodType> tag must be Method.

You need to provide very little (only the method name, its parameters, and the type) because the container will resolve it at runtime.






Copyright © 2000, SilverStream Software, Inc. All rights reserved.