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:
- Enterprise bean code should not reference anything outside of the enterprise bean modules. For example, you should not reference commands or data beans in the enterprise bean code.
- The previous sections describe how to include access control in a new bean when initially creating the bean. It can also be added after you have created your bean by adding the com.ibm.commerce.security.Protectable interface. If required, also add the com.ibm.commerce.security.Groupable interface to the enterprise bean's remote interface. Certain methods must be implemented in the bean as well. After adding these interfaces and adding required methods, regenerate the bean's deployed code and access bean. For more information, see Protectable interface.
- The instructions to create a new CMP enterprise bean below include sections to enable optimistic locking. The sections are:
- Create an optcounter column in your table and an optcounter field in your entity bean in step 1.
- Ensure the isolation level for the entity bean is read committed in step 2.
- Select the optimistic locking option in the entity bean's deployment descriptor in step 5.
- Calling super.ejbLoad and super.ejbStore methods in step 7.
- Ensure any finder methods that use a FOR UPDATE clause also include the WITH RS clause in step 8.
- Initializing the optcounter field in the ejbCreate method in step 10.
If desired, use the optimistic locking migration plug-in to enable optimistic locking in your new entity bean instead of performing the sections listed above. Use the optimistic locking migration plug-in after generating the access bean and before generating deploy code.
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.
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.Set the transaction isolation level
You must set the transaction isolation level for the bean to the correct value for your development database type.Set the security identity of the bean
To set the security identity of the bean:Set the security role of the bean
Set the security role for the methods in the bean.Set the optimistic locking option of the bean
If your bean will use optimistic locking, set the optimistic locking option in the deployment descriptor.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.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.Add new finders
If required, define new finders using the EJB deployment descriptor editor.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.Initialize optimistic locking fields in ejbCreate
To take advantage of optimistic locking, initialize the optCounter field in your entity bean in the ejbCreate method.Create a new ejbPostCreate method
Next, create a new ejbPostCreate method that has the same input parameters as the new ejbCreate method.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.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.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.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).Migrate EJB projects to use optimistic locking
Use the optimistic locking migration plug-in to enable optimistic locking in an EJB project.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 interfacesRelated concepts
Extending the WebSphere Commerce object model with entity beans
Extending the WebSphere Commerce object modelRelated reference
Column type differences between databases
Use of EJB 2.x entity beans