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:
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.
Follow these steps to create the Author bean:
CMP
2.x Entity bean
appears in the middle of the list.
CMP 2.x Entity bean
to reveal more choices. The other choice listed is CMP 1.x Entity
bean
.
CMP 2.x Entity bean
.
Since EJB beans are complex, this action launches the Create an Enterprise Bean wizard, allowing you to define the bean.
On the first page of the wizard, follow these steps:
UML EJB tutorial
.Author
ejbModule
.sample
2.x
.On the second page of the wizard, follow these steps:
sample.AuthorBean
.sample.AuthorLocalHome
.sample.AuthorLocal
.java.lang.Integer
.id
is on the list of CMP
attributes.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. 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.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.
Next, you will create the Book bean. Follow these steps:
Book
id
, of type java.lang.Integer
(the id
attribute is created for you automatically)title
, of type java.lang.String
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.
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..*
.
You may want to rearrange elements in the class diagram to see the details.
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:
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.
Next, you will modify some of the CMP attributes for the Author bean.
gender
of type java.lang.Character
.gender
attribute appears
in the Attribute compartment of the bean on the class diagram.
name
attribute in the Author bean.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 directoryNext, 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.
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.
Decoration | Meaning |
---|---|
![]() |
CMP 2.x entity bean |
![]() |
primary key field |
![]() |
CMP field, not primary |
![]() |
local interface |
![]() |
local home interface |
![]() |
remote interface |
![]() |
local method |
![]() |
local home method |
![]() |
remote method |
A complete list of decorations is shown in the information center under UML visualization reference.
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
()
and select Filters.
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.