Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Step 2: Implementing the CMP 2.0 Roster Entity Bean

The Roster entity bean, RosterEJB, creates a roster of students. It has a local home interface (LocalRosterHome) and a local component interface (LocalRoster).

Implementing the Local Home Interface: LocalRosterHome.com

LocalRosterHome declares the create(), findByPrimaryKey(), and additional finder methods. The findByScheduleID() method returns a collection based on the schedule ID; the findByStudentID() method returns a collection based on the student ID argument.

public interface LocalRosterHome extends EJBLocalHome
    public LocalRoster create(String rosterID, String scheduleID, String studentID)
throws CreateException; public LocalRoster findByPrimaryKey(String rosterID) throws FinderException; public Collection findByScheduleID(String scheduleID) throws FinderException; public Collection findByStudentID(String studentID) throws FinderException; }

Implementing the Local Component Interface: LocalRoster.java

The LocalRoster component declares three plain getter business methods that return persistent fields as shown next.

public interface LocalRoster extends EJBLocalObject
    public String fetchRosterID();
    public String fetchStudentID();
    public String fetchScheduleID();

Implementing the CMP 2.0 Entity Bean Class: RosterEJB.com

The code snippet that follows illustrates an abstract Roster entity bean class with several abstract accessor methods for CMP fields. Note that the business method, fetchRosterID(), depends on the abstract method getRosterID() to retrieve the CMP fields from the underlying database.

public abstract class RosterEJB implements EntityBean

    //accessor methods for cmp fields
    public abstract String getRosterID();
    public abstract void setRosterID(String rosterID);

    public abstract String getScheduleID();
    public abstract void setScheduleID(String scheduleID);

    public abstract String getStudentID();
    public abstract void setStudentID(String studentID);

    //business methods
    public String fetchRosterID ()
        return getRosterID();

    public String fetchStudentID()
        return getStudentID();

    public String fetchScheduleID()
        return getScheduleID();

The ejbCreate() method is shown next. It calls no data access calls but rather uses the setter methods to set the persistent fields.

public String ejbCreate (String rosterID, String scheduleID, String studentID)
    throws CreateException {

    return null;

The ejbPostCreate() method performs the JNDI lookup for the Student bean reference. It then uses the component interface to add the instance to the student instance object so it can be accessed by the student bean instance.

public void ejbPostCreate (String rosterID, String scheduleID, String studentID)
throws CreateException { System.out.println("RosterEJB ejbPostCreate...rosterID = "+rosterID); try { Context ic = new InitialContext(); LocalStudentHome home = (LocalStudentHome) ic.lookup("java:comp/env/ejb/StudentRef"); LocalStudent student = home.findByPrimaryKey(sid); student.addRoster((LocalRoster)context.getEJBLocalObject()); } catch (Exception ex) { context.setRollbackOnly(); ex.printStackTrace(); } }

Compiling LocalRosterHome.java, LocalRoster.java, and RosterEJB.java

If you are not already in the APPHOME\chapter11\cmp directory, change to this directory and run the compileRoster.bat batch file to compile the Roster bean class. The batch file generates LocalRosterHome.class, LocalRoster.class, and RosterEJB.class.

  • + Share This
  • 🔖 Save To Your Account