Bean Managed Persistence   «Prev  Next»
122
Lesson 3Writing ejbCreate() methods for bean-managed persistence
ObjectiveExplore the code for the ejbCreate() methods in an entity bean

Writing ejbCreate() methods

Each create() method in the home interface maps to an equivalent ejbCreate() method in the bean, just as they do for session beans. The methods are mapped through their parameters, which must match in type and number.

Primary Key is returned

Each ejbCreate() method, using its arguments, creates the underlying persistent entity object. The method uses JDBC, or some similar technique supported by the EJB platform, to create the data, and when finished, returns the primary key of the entity object that it created. The Customer example in this module uses a String as the primary key to make the code samples in the course more simple. This makes it easier to see how the code works. The String contains an integer that is used as the primary key for the new entity object. The following MouseOver illustrates the code:

ejbFindByPrimaryKey() is invoked by the container when the home interface findByPrimaryKey()
  1. The ejbFindByPrimaryKey() is invoked by the container when the home interface findByPrimaryKey() is invoked.
  2. selectByPrimaryKey() invokes the JDBC code to find the row using the information in the primary key. It returns true if the row is found, otherwise it returns false.
  3. A row was found, so it returns the primary key to the container.
  4. No row was found, so it return the ObjectNotFoundException to the client.
  5. If any exception is thrown by the JDBC code, it is caught and the EJBException is returned to the client. Notice that any message contained in the caught exception is passed back in the EJBException.

ejbCreate Method bmp
The ejbCreate(custnum, name) has a very similar code. The only difference is that the name parameter is stored in the newly created row.
The next lesson introduces the ejbPostCreate() method.