Create new entity beans

When you have a new attribute that needs to be added to the WebSphere Commerce object model, you can create a new database table with a column for the required attribute. You must then also include this attribute in an enterprise bean, so that WebSphere Commerce commands can access the information. One way to integrate the new attribute into the WebSphere Commerce object model is to create a new CMP enterprise bean. In this bean, you create a field that corresponds to the attribute in the new database table. Since WebSphere Commerce workspace provides a predefined EJB project for your new enterprise beans, you do not need to create an additional EJB project. Your new enterprise beans should be placed into the WebSphereCommerceServerExtensionsData EJB project. Later, when you deploy your customized beans, you create a WebSphereCommerceServerExtensionsData.jar JAR file and replace the existing JAR file in the WebSphere Commerce enterprise application running in WAS. By using this packaging convention, deployment is greatly simplified.

The following enterprise bean coding practices should be observed:

Tip: For detailed steps on creating a new entity bean, see the creating a new entity bean step of the Creating new business logic tutorial.

  1. Create a new CMP entity bean
    Create your new CMP entity bean using the Enterprise Bean Creation wizard. For each column in the corresponding database table, add a new CMP field to the bean.

  2. Set the transaction isolation level
    You must set the transaction isolation level for the bean to the correct value for your development database type.

  3. Set the security identity of the bean
    To set the security identity of the bean:

  4. Set the security role of the bean
    Set the security role for the methods in the bean.

  5. Set the optimistic locking option of the bean
    If your bean will use optimistic locking, set the optimistic locking option in the deployment descriptor.

  6. Modifying the entity context fields and methods
    The next step is to remove some of the fields and methods related to entity context that Rational Application Developer generates. WebSphere Commerce provides its own definition of these fields and methods, in the ECEntityBean base class, which your new bean should use.

  7. Modifying the ejbLoad and ejbStore methods
    When an entity bean is created, Rational Application Developer automatically generates a rudimentary ejbLoad method and a rudimentary ejbStore method, which you can customize in the course of your development. At this time, you will ensure that these methods implement the default behavior defined in the com.ibm.commerce.base.objects.ECEntityBean abstract class, which your entity bean extends.

  8. Add new finders
    If required, define new finders using the EJB deployment descriptor editor.

  9. Create a new ejbCreate method
    When the enterprise bean is created, the ejbCreate method is automatically generated. This method is then promoted to the remote interface, so that it is available in the access bean. The default ejbCreate method only contains parameters that are either the primary key or part of the primary key. This means that only those values are instantiated upon bean instantiation. If your enterprise bean contains fields that are not part of the primary key and are non-nullable fields, create a new ejbCreate method in which you specifically instantiate those fields. By doing so, each time a new record is created, all non-nullable fields will be populated with the appropriate data.

  10. Initialize optimistic locking fields in ejbCreate
    To take advantage of optimistic locking, initialize the optCounter field in your entity bean in the ejbCreate method.

  11. Create a new ejbPostCreate method
    Next, create a new ejbPostCreate method that has the same input parameters as the new ejbCreate method.

  12. Add access control methods to the bean
    If your new bean is to be protected under access control, add the getOwner method. Another method that is optional for access control purposes is the fulfills method. For details about required and optional methods, refer to Implementing access control in enterprise beans.

  13. Mapping the database table to the new enterprise bean
    After you create the new enterprise bean, create a mapping between the CMP fields in the bean and the columns in the database table. When both the enterprise bean and its corresponding database table exist, a "Meet-in-the-middle" type of mapping is used. Rational Application Developer provides tools to simplify this task.

  14. Modifying the schema name
    The next step is to modify the schema name so that your bean will be portable to other databases. The special value to allow a bean to be portable in this manner is NULLID.

  15. Create the access bean
    An access bean acts as a wrapper for the enterprise bean that simplifies how other components interact with the enterprise bean. You must create an access bean for your new enterprise bean. The tools in Rational Application Developer are used to generate this access bean, based upon the entity that you have already created (in particular, only methods that have been promoted to the remote interface will be used by the access bean).

  16. Migrate EJB projects to use optimistic locking
    Use the optimistic locking migration plug-in to enable optimistic locking in an EJB project.

  17. Generating deployed code
    The code generation utility analyzes the beans to ensure that Sun Microsystems' EJB specifications are met and it ensures that rules specific to the EJB server are followed. In addition, for each selected enterprise bean, the code-generation tool generates the home and EJBObject (remote) implementations and implementation classes for the home and remote interfaces, as well as the JDBC persister and finder classes for CMP beans. It also generates the Java ORB, stubs, and tie classes required for RMI access over IIOP, as well as stubs for the home and remote interfaces

Related concepts

Extending the WebSphere Commerce object model with entity beans
Extending the WebSphere Commerce object model

Related reference

Column type differences between databases
Use of EJB 2.x entity beans