Adding and removing CMP fields

Container-managed persistence (CMP) fields, or attributes, define the variables in the bean class for which the container must handle persistence management. You can use the Java™ EE tools to add a CMP field with the EJB deployment descriptor editor.

About this task

When you initially create a CMP entity bean, you have the option of adding persistence fields, or attributes, to the bean.

However, if you later decide that you want to add a CMP field to the entity bean and perhaps define it as a key field, you can use the EJB deployment descriptor editor. For example, if you created a CMP entity bean named Customer, you might later want to add the CMP field e-mail to the entity bean. Use the Bean page of the EJB deployment descriptor editor to add and remove CMP fields.

Note: If a relationship exists between one or more CMP beans, any changes to the key shape of one CMP bean result in the related CMP bean being regenerated.

Procedure

  1. In the Enterprise Explorer view of the Java EE perspective, right-click the deployment descriptor for your EJB project and select Open With > Deployment Descriptor Editor.
  2. On the Bean page of the editor, select the CMP entity bean that you want to work with.
  3. Click the Add button next to the CMP Fields section. The CMP Fields dialog box opens and lists the current CMP fields.
    Note: If the enterprise bean is binary (red color), you are not allowed to add a CMP field for that bean.
  4. Click Add.
  5. In the Name field, specify a name for the CMP field, making sure that the name is unique for the entity bean class.
  6. In the Type field, specify a type for the CMP field either by typing in the name of the type or by selecting a type from the drop-down list. The drop-down contains a list of types, including the Java primitive types. You can also click Browse to select other types that exist on the class path of the EJB project. If you type the class name of a type that is in the list, the fully qualified name displays.
  7. If you want the CMP field to be an array, select the Array check box, then specify the number of dimensions for the array.
  8. To define the CMP field as a key field, select the Key field check box. This is not an option if this is a child bean. A key field is required for a CMP bean. The CMP key field is added to both the bean and key classes. The create method of the Home interface is modified to include this new type.
  9. For EJB 1.1 beans, the following options are available:
    • To generate methods to retrieve and set values for the CMP field, select the Access with getter and setter methods check box.
    • To promote these generated methods to the remote interface, select Promote getter and setter methods to remote interface. This option is only available if you selected Access with getter and setter methods.
    • To specify the getter method as read-only, select Make getter read-only. This informs a WebSphere® Application Server container that the getter method does not update any of the CMP fields of the CMP entity bean. The container will not write the bean data back to the database after the getter method is called, which can improve performance. This option is only available if you selected Promote getter and setter methods to remote interface.
      Tip: When selecting the Make getter read-only option, the workbench automatically adds an entry to an EJB 1.x read access intent for your newly created CMP attribute. You can find this entry by going into the Access page of the EJB deployment descriptor editor, see the Access Intent for Entities 1.x section. For details on modifying the EJB 1.x read access intent, see Adding access intent for entity 1.x beans.
  10. For EJB 2.x beans, the following options are available if you are not creating a key field:
    • Promote getter and setter methods to remote interface
    • Promote getter and setter methods to local interface
  11. Click Apply, and repeat steps 4 through 11 for additional CMP fields, then click Close.
  12. Click Finish. The new CMP fields are added to the bean definition.

Results

Note: To remove a CMP field, select the field on the Beans page of the EJB deployment descriptor editor and click the Remove button.

Feedback