- Advantages of CMP Entity Beans over BMP Entity Beans
- CMP 2.0 Entity Bean Sample Application
- Step 1: Implementing the CMP 2.0 Address Entity Bean
- Step 2: Implementing the CMP 2.0 Roster Entity Bean
- Step 3: Implementing the CMP 2.0 Student Entity Bean
- Step 4: Implementing JSP Clients to Test CMP 2.0 Entity Beans
- Step 5: Packaging the CMP 2.0 Entity Beans as an ejb-jar File
- Step 6: Packaging the JSP As a Web Component
- Step 7: Deploying the CMP 2.0 Entity Bean Sample Application
- Step 8: Testing the Sample Application
- A Discussion of the Deployment Descriptor
- Summary
Step 1: Implementing the CMP 2.0 Address Entity Bean
First, implement the local interfacesLocalAddress, LocalAddressHome, and the AddressEJB entity bean classes. The AddressEJB bean is used to create and manage address-related information.
Implementing the Local Home Interface: LocalAddressHome.java
The LocalAddressHome interface declares two methods: create() and the mandatory findByPrimaryKey(). Notice that both methods return the LocalAddress interface and must not throw RemoteExceptions, as shown in the following example:
public interface LocalAddressHome extends EJBLocalHome { public LocalAddress create (String customerID, String addressID,
String street, String city, String zip, String state) throws CreateException; public LocalAddress findByPrimaryKey(String addressID) throws FinderException; }
Implementing the Local Component Interface: LocalAddress.java
The LocalAddress interface declares basic getter methods to retrieve the CMP address fields from the bean class:
public interface LocalAddress extends EJBLocalObject { public String getAddressID(); public String getStreet(); public String getCity(); public String getZip(); public String getState(); }
Implementing the CMP 2.0 Entity Bean Class: AddressEJB.com
The CMP 2.0 entity bean class must be declared abstract; it extends the EntityBean interface. Notice that the abstract methods access the container-managed persistent fields such as addressID, street, city, zip code, and state:
public abstract class AddressEJB implements EntityBean { private EntityContext context; //access methods for cmp fields public abstract String getAddressID(); //primary key public abstract void setAddressID(String id); public abstract String getStreet(); public abstract void setStreet(String street); public abstract String getCity(); public abstract void setCity(String city); public abstract String getZip(); public abstract void setZip(String zip); public abstract String getState(); public abstract void setState(String state);
The ejbCreate() method uses the abstract setter methods to set the persistent fields. Please refer to Chapter 9 for a detailed discussion and rules on writing ejbCreate() and ejbPostCreate() methods. The container automatically saves the fields in the database table. The ejbCreate() method returns a String primary key, as shown here:
public String ejbCreate (String sid,String id,String street,
String city,String zip,String state) throws CreateException { setAddressID(id); setStreet(street); setCity(city); setZip(zip); setState(state); return null;//explanation at the end of the chapter }
Look at the ejbPostCreate() method; it looks up the student object and adds the roster object to the student list. This allows the StudentEJB instance to access AddressEJB.
public void ejbPostCreate (String sid,String id,String street,String city,String zip,String state) throws CreateException { try { Context ic = new InitialContext(); LocalStudentHome home = (LocalStudentHome) ic.lookup("java:comp/env/ejb/StudentRef"); LocalStudent student = home.findByPrimaryKey(sid); student.addAddress((LocalAddress)context.getEJBLocalObject()); } catch (Exception ex) { context.setRollbackOnly(); ex.printStackTrace(); } }
Compiling LocalAddressHome.java, LocalAddress.java, and AddressEJB.java
In you windows terminal, change directory to APPHOME\chapter11\cmp and run compileAddress.bat to generate the LocalAddressHome.class, LocalAddress.class, and AddressEJB.class in your directory.