Container Managed Persistence - CMP

Determine the persistent classes

In 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 Primitive type values representing the ID, title, artist, type and notes.