Implementing Finder Methods

   

When a client calls a finder method, the container calls your corresponding ejbFind method. The most important concept here is that you're only responsible for returning the primary key values that match the criteria of a finder from an ejbFind method. The container responds by returning component interface references to the client, but it doesn't have to activate any entity object located by the finder until the client calls a business method on its component interface. This means that simply invoking a finder method that applies to a particular entity object doesn't cause ejbLoad to be called for that object.

The simplest finder method is findByPrimaryKey . Given the preceding discussion, the only responsibility you really have in an ejbFindByPrimaryKey method is to make sure that the primary key that's passed in corresponds to an entry in the database. Listing 6.11 shows this method for EnglishAuctionBean .

Listing 6.11 ejbFindByPrimaryKey “A Finder Method by Primary Key
 package com.que.ejb20.auction.model;  ...  public class EnglishAuctionBean extends AbstractEntity implements EntityBean {   ...    public Integer ejbFindByPrimaryKey(Integer primaryKey)     throws FinderException {     // throw an application exception if the primary key isn't passed      if (primaryKey == null) {       throw new FinderException("Must specify a non-null primary key");      }      Connection con = null;      PreparedStatement stmt = null;      ResultSet rs = null;      try {       con = BMPHelper.getConnection("auctionSource");        // perform a query to see if the primary key is valid        stmt = con.prepareStatement("SELECT id FROM auction WHERE id = ?");        stmt.setInt(1, primaryKey.intValue());        rs = stmt.executeQuery();        boolean found = rs.next();        if (!found) {         // the requested object doesn't exist          throw new ObjectNotFoundException("Cannot find auction " + primaryKey);        }      }      catch (SQLException e) {       // throw a system exception if a database access error occurs        throw new EJBException;      }      finally {       // close the connection        BMPHelper.cleanup(stmt, con);      }      // the requested auction was found so return the primary key      return primaryKey;    }    ...  } 

Listing 6.12 shows a slightly more complex finder method implementation that retrieves the primary keys for all the auctions defined in the database and returns them as a collection of Integer objects. Finder method implementations are basically the same with the significant difference being in the select statement they execute.

Listing 6.12 ejbFindAllAuctions “A Finder Method For Retrieving All Auctions
 package com.que.ejb20.auction.model;  ...  public class EnglishAuctionBean extends AbstractEntity implements EntityBean {   ...    public Collection ejbFindAllAuctions() throws FinderException {     Connection con = null;      PreparedStatement stmt = null;      ResultSet rs = null;      Collection keys = new ArrayList();      try {       con = BMPHelper.getConnection("auctionSource");        // perform a query to select all the primary key values        stmt = con.prepareStatement("SELECT id FROM auction");        rs = stmt.executeQuery();        while (rs.next()) {         Integer pk = (Integer)rs.getObject("id");          keys.add(pk);        }      }      catch (SQLException e) {       // throw a system exception if a database access error occurs        throw new EJBException;      }      finally {       // close the connection        BMPHelper.cleanup(stmt, con);      }      // return the primary keys      return keys;    }    ...  } 


Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net