Home > Articles > Programming > Java

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

This chapter is from the book

Step 3: Implementing the CMP 2.0 Student Entity Bean

The student entity bean provides the logic to create a student entity. It accesses the address bean and roster bean instance to look up the address entity assigned to the student and the classes for which the student is registered.

Implementing the Local Home Interface: LocalStudentHome.com

The local home interface declares the create() method and several finder methods. The create() method creates the student entity; the finder methods allow clients to query by primary key, student's first name, and last name.

public interface LocalStudentHome extends EJBLocalHome
{
    public LocalStudent create (String StudentID, String firstName, String lastName)
throws CreateException; public Collection findByLastName (String lastName) throws FinderException; public Collection findByFirstName (String firstName) throws FinderException; public LocalStudent findByPrimaryKey (String StudentID) throws FinderException; }

Implementing the Local Component Interface: LocalStudent.java

The local component interface declares the getter methods. In addition, several business methods [including getAddressList() and getRosterList()] return a collection of LocalAddress and LocalRoster objects to the client. The client uses getAddressList() and getRosterList() to retrieve multiple addresses and the classes for which the student is registered. The addAddress() and addRoster() methods add local components to an existing collection.

public interface LocalStudent extends EJBLocalObject
{
    public String getStudentID();
    public String getFirstName();
    public String getLastName();
    public ArrayList getAddressList();
    public ArrayList getRosterList();
    public void addAddress(LocalAddress address);
    public void addRoster(LocalRoster roster);
}

Implementing the CMP 2.0 Entity Bean Class: StudentEJB.com

The student entity bean class uses abstract methods to declare several CMP fields, including studentID, firstName, and lastName. The unidirectional, one-to-many relationship between the student bean and address bean is represented by the CMR field addresses defined by the getAddresses() and setAddresses() abstract methods. Similarly, the rosters represent the unidirectional one-to-many relationships between student and roster beans.

public abstract class StudentEJB implements EntityBean {

    //access methods for cmp fields
    public abstract String getStudentID();      //primary key
    public abstract void setStudentID(String id);

    public abstract String getFirstName();
    public abstract void setFirstName(String firstName);

    public abstract String getLastName();
    public abstract void setLastName(String lastName);

The StudentEJB class declares two CMR fields to define its one-to-many relationships with the AddressEJB and RosterEJB bean classes. The getAddresses() and getRosters() abstract methods return collections of local component objects of the respective bean type. The setAddresses() and setRosters() abstract methods take collection arguments of the local components of the respective bean type, as shown in the code fragment that follows.

//abstract cmr field methods for address entity bean
public abstract Collection getAddresses();
public abstract void setAddresses (Collection addresses);

//abstract cmr field methods for roster entity bean
public abstract Collection getRosters();
public abstract void setRosters(Collection rosters);

The business method getAddressList() calls the CMR abstract method getAddresses(), which returns a collection. The getAddressList() method then extracts the local component interface LocalAdress, creates an array list using the Iterator, and returns it to the client. The client can then invoke business methods on individual local components. The addAddress() method is used by the client to add a local component interface, LocalAddress, to the collection of LocalAddress objects. The getRosterList() and addRoster() methods (not shown here) mirror the same functionality for the roster bean. When a client invokes getAddressList(), it receives a list of LocalAddress interfaces and uses it to invoke business methods.

//business methods
public ArrayList getAddressList() {
    ArrayList list = new ArrayList();
    Iterator c = getAddresses().iterator();
    while (c.hasNext()) {
        list.add((LocalAddress)c.next());
    }
    return list;
}
public void addAddress (LocalAddress address) {
    getAddresses().add(address);
}

The ejbCreate() and ejbPostCreate() methods in the StudentEJB class are fairly simple. The ejbCreate() method invokes the abstract methods to set the id, firstName, and lastName abstract persistent fields. The ejbPostCreate() method is left empty.

public String ejbCreate (String id, String firstName, String lastName)
    throws CreateException {
    System.out.println(" -- StudentEJB - ejbCreate...");
    setStudentID(id);
    setFirstName(firstName);
    setLastName(lastName);

    return id;
}

public void ejbPostCreate (String id, String firstName, String lastName)
    throws CreateException {
    System.out.println("StudentEJB -ejbPostCreate(" +
            id + ", " + firstName + ", " + lastName + ")...");
}

Compiling LocalStudentHome.java, LocalStudent.java, and StudentEJB.java

If you are not already in the APPHOME\chapter11\cmp directory, change to this directory and run the compileStudent.bat file to compile the Student bean class. The batch file creates LocalStudentHome.class, LocalStudent.class, and StudentEJB.class.

  • + Share This
  • 🔖 Save To Your Account