|
|
Step 1: determine the persistent classes
n this simple example we will use two Enterprise JavaBeans. The first, called
`CD' models a music CD. It contains attributes (instance variables) that store
the title ID code and various other properties of a music CD. The second is
called `CDCollection', and models a collection of such CDs. This bean acts as a
simple interface between the client and the CD Bean; technically we could
manage without it but it does make certain operations easy to follow. The
CDCollection Bean will have the following methods deleteAll(), addCd(),
findInAnyField() and findAll().
|
addCd();
|
Adds a single CD specified by values of its attributes
|
|
deleteAll();
|
Delete all CDs
|
|
findInAnyField()
|
Returns an array of CD instances which have the specified text string in any
of their attributes
|
|
findAll()
|
Returns an array of all CD instances in the system
|
All these methods could be implemented by direct manipulation of the home
interface of the CD Bean, but it is slightly more elegant to do it this way.
Because the CDCollection Bean only interacts with the CD Beans during requests
from clients, it appears to have no persistent information. So it is a session
Bean. Moreover, since each method is completely self-contained, it is a
stateless session bean.
The CD Bean, however, will be an entity EJB because some of its information is
persistent. For example, the ID, artist, title, type and notes about the
recording will all be persistent. Of course the CD Bean may have other
instance variables, but they won't necessarily be persistent. For example, some
will hold temporary data, and others will be derived from the persistent
attributes.
In this example, I will assume that the persistent fields are all Java `String'
values representing the ID, title, artist, type and notes. You could, of
course, add other information you feel to be important.
To use container-managed persistence, a `primary key' needs to be defined for
the entity Bean. The primary key is used to distinguish between objects that
may be identical in other respects. The primary key will usually be one of the
attributes of the object. In principle a primary key can be a combination of
attributes, but using a single attribute is simpler. I will use the `ID'
attribute of the CD Bean as the primary key, as it is the only field that has
to be unique.
|