Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Configuring a Query for an EJB 2.1 Entity Bean With Bean-Managed Persistence

You must implement an ejbFindByPrimaryKey method for an entity bean with bean-managed persistence (see "Implementing an ejbFindByPrimaryKey Method for an EJB 2.1 Entity Bean With Bean-Managed Persistence"). Optionally, you may configure other finders (see "Implementing Other Finder Methods for a EJB 2.1 Entity Bean With Bean-Managed Persistence").

For more information, see "Implementing EJB 2.1 Queries".

Implementing an ejbFindByPrimaryKey Method for an EJB 2.1 Entity Bean With Bean-Managed Persistence

The ejbFindByPrimaryKey implementation is a requirement for all entity beans with bean-managed persistence. Its primary responsibility is to ensure that the primary key corresponds to a valid bean. Once it is validated, it returns the primary key to the container, which uses the key to return the bean reference to the user.

This sample verifies that the employee number is valid and returns the primary key, which is the employee number, to the container. A more complex verification would be necessary if the primary key was a class.

public EmployeePK ejbFindByPrimaryKey(EmployeePK pk)
        throws FinderException {
        if (pk == null || pk.empNo == null) {
            throw new FinderException("Primary key cannot be null");
        }
        try {
            conn = getConnection(dsName);
            ps = conn.prepareStatement(findByPKStatement);
            ps.setInt(1, pk.empNo.intValue());
            ps.executeQuery();
            ResultSet rs = ps.getResultSet();
            if (rs.next()) {
                pk.empNo = new Integer(rs.getInt(1));
                pk.empName = new String(rs.getString(2));
                pk.salary = new Float(rs.getFloat(3));
            } 
            else {
                throw new FinderException("Failed to select this PK");
            }
        } 
        catch (SQLException e) {
            throw new FinderException(e.getMessage());
        } 
        catch (NamingException e) {
            System.out.println("Caught an exception 1 " + e.getMessage() );
            throw new EJBException(e.getMessage());
        } 
        finally {
            try {
                ps.close();
                conn.close();
            } 
            catch (SQLException e) {
                throw new EJBException(e.getMessage());
            }
        }
        return pk;
    }

Implementing Other Finder Methods for a EJB 2.1 Entity Bean With Bean-Managed Persistence

Optionally, you can create other finder methods in addition to the single ejbFindByPrimaryKey method.

To create other finder methods, do the following:

  1. Add the finder method to the home interface.

  2. Implement the finder method in the bean implementation of an entity bean with bean-managed persistence.

Finders can retrieve one or more beans according to the WHERE clause. If more than a single bean is returned, then a Collection of primary keys must be returned by the bean's finder method. These finder methods need only to gather the primary keys for all of the entity beans that should be returned to the user. The container maps the primary keys to references to each entity bean within either a Collection (if multiple references are returned) or to the single class type.

The following example shows the implementation of a finder method that returns all employee records.

public Collection ejbFindAll() throws FinderException {
  ArrayList recs = new ArrayList();

  ps = conn.prepareStatement("SELECT EMPNO FROM EMPLOYEEBEAN");
  ps.executeQuery();
  ResultSet rs = ps.getResultSet();

  int i = 0;

  while (rs.next()) {
   retEmpNo = new Integer(rs.getInt(1));
   recs.add(retEmpNo);
  }

  ps.close();
  return recs;
}