Exercise 1.3: Adding entity beans to class diagrams

Before you begin, you must complete Exercise 1.2: Preparing the workspace.

Exercise 1.3 guides you through the creation of entity beans from a UML class diagram. In this module, you will do these tasks:

Description of the beans

You will create two entity beans in this scenario: Author and Book. The Author bean will represent a database table, also named AUTHOR. The bean will contain fields representing the author: ID, name, age and gender. These fields in the bean will map directly to columns in the AUTHOR table.

The Book bean will represent a book. Its fields include ID and title. Data corresponding to the Book bean will be stored in the BOOK table.

Creating the Author bean

Follow these steps to create the Author bean:

  1. Locate the EJB drawer of the palette on the right of the UML class diagram.
  2. Click on the EJB drawer to open it if it is not already open. CMP 2.x Entity bean appears in the middle of the list.
  3. Click the arrow (pull down menu icon) to the right of CMP 2.x Entity bean to reveal more choices. The other choice listed is CMP 1.x Entity bean.
  4. Select the CMP 2.x Entity bean.
  5. Move the mouse pointer onto the class diagram and click where you want the bean placed. This creates a new CMP 2.x entity bean and visualize it in the class diagram.

Since EJB beans are complex, this action launches the Create an Enterprise Bean wizard, allowing you to define the bean.

The Create an Enterprise Bean page

On the first page of the wizard, follow these steps:

  1. Verify that the EJB project is UML EJB tutorial.
  2. Provide the Bean name, Author
  3. Verify the Source folder is ejbModule.
  4. Change the Default package name to sample
  5. Verify that the CMP Version is 2.x.
  6. Click Next.

basic properties page of new EJB wizard

The Enterprise Bean Details page

On the second page of the wizard, follow these steps:

  1. Leave the Bean supertype blank.
  2. Verify that Bean class is sample.AuthorBean.
  3. Leave the Remote client view check box cleared.
  4. Select the Local client view check box.
  5. Verify the Local home interface name is sample.AuthorLocalHome.
  6. Verify the Local interface name is sample.AuthorLocal.
  7. Verify the Key class is java.lang.Integer.
  8. Select the Use the single key attribute type for the key class check box.
  9. Verify that the key id is on the list of CMP attributes.
  10. Add two CMP attributes:
    1. Click Add.
    2. Create an attribute named name with a type of java.lang.String. Leave the Array and Key field check boxes cleared and the Promote getter and setter methods to local interface check box selected. When getters and setters are promoted to the local interface, they are exposed to client applications.
    3. Click Apply.
    4. Create an attribute named age with a type of java.lang.Integer. Leave the Array and Key field check boxes cleared and the Promote getter and setter methods to local interface check box selected.
    5. Click Apply, then Close.
    6. Click Finish.

the enterprise bean details page of the new EJB wizard

Your first entity bean will be created and visualized on the class diagram. The following files appear in the Project Explorer view:

Open the EJB deployment descriptor and notice that on the Overview page, under the Enterprise JavaBeans heading, the Author bean is listed. Click on the Author bean entry. The Bean page of the deployment descriptor editor opens, showing the three CMP fields and the four class and interface files, including the class used by the primary key, java.lang.Integer.

On the Bean page, under the WebSphere Bindings heading, the JNDI name ejb/sample/AuthorLocalHome is shown. This JNDI name is used when a client application is searching for the bean.

In the Project Explorer view, under the EJB project, the ejbModule > META-INF directory contains the new file ibm-ejb-jar-bnd.xmi. This file contains IBM-specific binding information. The information within it is accessible through the deployment descriptor editor, but the values are stored a separate file to allow you easily remove IBM-specific enhancements if you want to deploy your application on another vendor's EJB server. Later in the EJB development process, the file ibm-ejb-jar-ext.xmi will appear. It contains IBM-specific extension information.

Also in the Project Explorer view, expand the deployment descriptor directory under the EJB project, expand Entity Beans > Author and explore the links to the various components that comprise the Author bean.

Creating the Book bean

Next, you will create the Book bean. Follow these steps:

  1. Add another CMP 2.x entity bean to the class diagram.
  2. Follow the same steps as for the Author bean, with these differences:
  3. Click Finish.

Your second entity bean is created and visualized on the class diagram and the following files appear in the Project Explorer view:

The EJB deployment descriptor now has Book on the list of Enterprise JavaBeans on the Overview page. The Book bean has details similar to the Author bean. From the Project Explorer view, expand the deployment descriptor directory under the EJB project. Expand Entity Beans > Book and explore the links to the bean's components.

Defining relationships between beans

Next, you will define a CMP relationship between the two CMP beans on the class diagram. A single Author can be associated with zero or more books. And a single book can be associated with one or more authors. This relationship would be expressed as a bidirectional many-to-many relationship using the notation 0..*:0..*.

  1. Double-click the title bar of the UML class diagram to enlarge the window.
  2. In the palette view, expand the CMP relationship drawer by clicking the arrow (pull down menu icon).
  3. Select the 0..*:0..* CMP relationship.
  4. Move the mouse pointer over the Author bean. You will see an arrow icon (arrow icon) if the relationship is valid for the bean. If the relationship is not valid, you will see the same icon plus an invalid symbol (invalid arrow icon) .
  5. Click on the Author bean.
  6. Drag the cursor to the Book bean and release it. If this target bean is invalid for the relationship, the invalid icon (invalid arrow icon) appears.
  7. The CMP relationship is visualized as an association on the diagram after the appropriate underlying code is generated. In the deployment descriptor, these relationships appear under the Relationships heading on the Bean page.
  8. Select Ctrl + S to save the class diagram.

Relationships section of EJB deployment descriptor

You may want to rearrange elements in the class diagram to see the details.

class diagram with relationships shown

Exploring additional views

The Outline and Properties views provide useful information on the overall class diagram and on specific EJB component properties.

The Outline view appears, by default, in the bottom left of the workbench. Use this view to navigate a large diagram following these steps:

  1. If the class diagram is still using the entire workbench frame, double-click the title bar of the class diagram again to restore it to normal size.
  2. Notice the Outline view in the bottom left corner of the workbench. The class diagram is displayed there on a small scale. If the diagram is too large to see completely in the edit window, you will see a highlighted rectangle in the Outline view which indicates the visible portion of the diagram.
  3. In the Outline view, move the highlighted rectangle around with your mouse pointer. The section of the class diagram that is visible in the editor area moves to correspond to the highlighted section in the Outline view.

Outline view of a class diagram

The Properties view appears, by default, in the bottom right of the workbench. It has two tabs, Appearance and Advanced. You can change colors, fonts and other visual properties on the Appearance page. The Advanced page shows detailed properties of the highlighted element in the class diagram. Some of the property values in the Properties view are read-only and some can be modified.

Properties view of a class diagram

Modifying attributes from the UML class diagram

Next, you will modify some of the CMP attributes for the Author bean.

Add a CMP attribute to a bean

  1. Move the mouse cursor anywhere on the Author bean. An Action bar appears.

  2. Action bar
  3. Click the primary key symbol (primary key field icon) in the Action bar to add additional CMP attributes to AuthorBean. The CMP Fields wizard opens.
  4. Add a field with the name gender of type java.lang.Character.
  5. Click Apply > Close > Finish. The bean classes are updated and the gender attribute appears in the Attribute compartment of the bean on the class diagram.

CMP attributes for the Author bean

Make a CMP attribute part of the key

  1. Highlight the name attribute in the Author bean.
  2. Right-click and select Edit > Add to Key. The New Key class wizard opens. A new key class is now required because the key is no longer a primitive Java type. Prior to this step, the java.lang.Integer was used as the key class.
  3. Click OK to use the preselected options to create the new key class with the default key class name, AuthorKey. The name attribute now appears on the class diagram with the primary key field symbol. The new key class appears in the UML EJB tutorialClient > ejbModule > sample directory
  4. Select Ctrl + S to save the class diagram.

New Key class wizard

Manipulating objects in the UML class diagram

Next, look at the details of the class diagram. Double-click the title bar of the class diagram to enlarge it. Now the class diagram editor takes up most of the workbench window, leaving the palette expanded on the right.

Rearrange the bean components on the diagram

  1. Click once on one of the beans. Small black squares appear around the bean's rectangle, indicating that it has been selected.
  2. Drag the bean to another location on the class diagram and drop it. If the Author or Book beans are moved, the relationship arrow adjusts to reflect the new position.

Change the items displayed for a bean

  1. In the class diagram editor, highlight the Author bean. The rectangle representing the bean is divided into several compartments. The default compartments that are displayed are Attribute and Provided Interfaces. The compartments have a hide icon (hide icon) in the top left corner.
  2. Click on one of the hide icons to hide that compartment of the bean. The compartment collapses and the hide icon is replaced with the show icon (show icon).
  3. Click on the show icon to re-expand that compartment.
  4. Select which compartments are shown or hidden by right-clicking the bean and selecting Filters > Show/Hide Compartment.

EJB with hidden and shown sections

Open editors from the diagram

When you open the EJB Bean Java Editor, an Outline view for this editor appears in the lower left corner of the workbench. You can make changes to the bean from this view. Right-click one of the methods in the Outline view and select Enterprise Bean. Select one of the Promote or Demote choices to promote or demote the selected method to one of the interface types listed.

Try adding a method to the class using the EJB Bean Java Editor. The new method appears in the Outline view. If you promote the new method to an interface, the method appears on the class diagram.

EJB Bean Java Editor

Familiarize yourself with the decorations associated with the beans

Decoration Meaning
CMP 2.x entity bean decoration CMP 2.x entity bean
primary key field decoration primary key field
CMP field decoration CMP field, not primary
local interface decoration local interface
local home interface decoration local home interface
remote interface decoration remote interface
local method decoration local method
local home method decoration local home method
remote method decoration remote method

A complete list of decorations is shown in the information center under UML visualization reference.

UML class diagrams and code interaction

As you make changes in your class diagram, corresponding changes are automatically made to the underlying code. Conversely, if you make changes directly to the code, the class diagram is automatically altered to reflect the change. Any type of change made within the workbench results in automatic and immediate revalidation of the code. For EJB applications, validation includes identifying compilation errors in Java classes, class path problems, and violation of EJB specifications.

At the bottom of the workbench, several views are stacked, including one named Problems. This view shows all errors, warnings and informational messages for all projects and files within the workbench. Double-clicking one of the listed problems opens the appropriate file to the place where the error occurs.

The Problems view limits the number of problems listed to 100 by default. You can change this limit, or filter the view to display only items you want to see. Filtering options include problem severity, projects, resources, and problem type. To modify these default settings, click the arrow (pull down menu icon) and select Filters.

Configuring preferences for UML class diagrams

You can change some of the default behaviors and display characteristics of class diagrams. From the Window menu, select Preferences. Expand Modeling to see the items that can be changed.

The main section contains Global Settings, such as whether Action Bars appear when you hold the mouse pointer over a class in the diagram. The Appearance section has several pages that allow you to change things like fonts, colors, connectors, and shapes. The EJB section has a settings for when class diagrams open. Other sections include options for Java fields and methods, rules and grids, and validation.

Step through the sections to become familiar with the various options. After making changes, click OK to save them and exit the Preferences page.

Now you are ready to begin Exercise 1.4: Mapping entity beans to relational databases.

Terms of use | Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.