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.

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 email 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 may result in the related CMP bean being regenerated.

To add a CMP field with the EJB deployment descriptor editor:

  1. On the Beans page of the EJB deployment descriptor editor, select the CMP entity bean.

  2. 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 will not be allowed to add a CMP field for that bean.

  3. Click Add.

  4. In the Name field, specify a name for the CMP field, making sure that the name is unique for the entity bean class.

  5. 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.

  6. If you want the CMP field to be an array, select the Array check box, then specify the number of dimensions for the array.

  7. To specify an initial value for the CMP field, type the value in the Initial Value field. This option is not available for 2.0 CMP beans. For simple string types, the value must be enclosed in double quotation marks; for example: "string-type". Char types must be enclosed in single quotes; for example: 'c'.

    For an array, the value must correctly use braces; for example:

    • {1,2,3} for a 1-dimension array

    • {{1,2,3}, {4,5}} for a 2-dimension array.

  8. To define the CMP field as a key field, select the Key field check box. This will not be 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.

  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 the steps to for additional CMP fields, then click Close.

  12. Click Finish. The new CMP fields are added to the bean definition.

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

 

Related concepts

Key classes and key fields

 

Related tasks

Adding client views or interfaces to beans