Chapter 11. Business logic 287
򐂰 Type Customer for the Bean name.
򐂰 Verify that the Source folder field is set to ejbModule; otherwise, click Browse
to select the source folder.
򐂰 Set the Default package to itso.ad.business.ejb.model.
Figure 11-31 Create a CMP 2.0 bean
򐂰 Click Next to display the Enterprise Bean Details panel (Figure 11-32).
򐂰 In the Bean supertype drop-down list, accept <none>.
򐂰 Accept the names given for the Bean class, EJB binding name, and Key
class.
򐂰 Accept the selected Local client view check box and the names for Local
home interface and Local interface.
򐂰 Do not select the Remote client view check box. For this bean, we do not use
the remote capabilities at the entity level.
288 WebSphere Version 5 Application Development Handbook
Figure 11-32 Create CMP 2.0 details
Create attributes
Next we create the CMP fields for the Customer bean. Click Add next to the CMP
attributes list box and the CMP Attribute panel opens (Figure 11-33).
Figure 11-33 Create a CMP attribute (customerId field and name field)
Chapter 11. Business logic 289
򐂰 Define each field as shown in Table 11-8:
Enter the field name and the field type.
–Select Key field for the customerId attribute.
Deselect Promote getter and setter methods to local interface for the name
attribute. We use the composite value (transfer object) to obtain the data of
the Customer bean, and therefore we do not promote the getter and setter
methods to the local interface.
–Click Apply for each field and click Close when finished.
Table 11-8 List of CMP fields for the customer bean
If the key field type is a single value Java class, for example, java.lang.String,
select Use the single key attribute type for the key class. Such a selection
prevents the Application Developer from generating a key wrapper class.
A compound key or a Java primitive (int) always requires a wrapper class.
Figure 11-34 shows the CMP attribute list with all the attributes defined and the
single key attribute type as key class.
Attribute Type Key field Promote getter and setter to
local interface
customerId java.lang.String Yes ---
name java.math.BigDecimal No No
Notes:
򐂰 The attribute name must begin with a lowercase letter (EJB specification).
򐂰 CMP attributes can be Java primitive types, for example, int.
򐂰 CMP attributes can be primitive wrappers (Integer, Long, Double).
򐂰 CMP attributes can be serializable classes that map naturally to fields in
relational database (java.lang.String, java.sql.Timestamp,
java.util.Date).
򐂰 We can also define our own serializable JavaBeans and use them as
complex fields (dependent value class).
290 WebSphere Version 5 Application Development Handbook
Figure 11-34 CMP attributes list
򐂰 Click Next to open the EJB Java Class Details window.
򐂰 In our case, no further information is required (no superclass for the bean, no
extensions to the component interface).
򐂰 Click Finish to generate the bean class and interfaces.
Generated classes
As a result of the creation process, the Application Developer generates three
classes for the Customer bean:
򐂰 CustomerBean—Abstract class that defines the attributes of the bean
򐂰 CutomerLocalHome—Home interface with create and find methods
򐂰 CustomerLocal—Component interface with methods available to a client
Create the CmpEntityAdapter
Next we create the abstract CmpEntityAdapter base class for the CMP entity
beans (see “Creating the classes” on page 264 for more details on how to create
the class), and move the generated EJB callback methods from the
CustomerBean into it. Note that the generated ejbCreate and ejbPostCreate
methods are unique to the respective bean class and cannot be moved.
򐂰 Expand the ItsoAdEJB project in the Java Package Explorer view.
򐂰 Select the itso.ad.business.ejb.model package and New -> Class (context).
򐂰 Type CmpEntityAdapter as the name and select public and abstract for the
Modifiers (Figure 11-35).
򐂰 Add javax.ejb.EntityBean to the Interfaces list.
򐂰 Be sure to deselect the Inherited abstract methods select box.
򐂰 Click Finish. The CmpEntityAdapter Java code is opened in the Java editor.
Chapter 11. Business logic 291
Figure 11-35 Create the CmpEntityAdapter
򐂰 Open the CustomerBean class in the Java editor (Package Explorer view).
򐂰 In the Outline view, select the EJB callback methods and instance variables
(except the ejbCreate and ejbPostCreate methods) and Cut (Figure 11-36).
Figure 11-36 Cut the EJB callback methods

Get WebSphere Version 5 Application Development Handbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.