# How Do I: Define a Container-Managed Persistence (CMP) Field?

Container-managed persistence (CMP) fields are virtual fields in an entity bean. These fields are not defined in the entity bean itself but correspond to columns in the database table. The entity bean only implements the accessor (getter and setter) methods.

To Define a CMP Field

1. Make sure that the entity bean is displayed in Design View.
2. Right-click the right-hand area of the bean, and choose Add CMP Field. At the bottom of the bean, underneath CMP Fields, a new CMP field appears.
3. Enter an appropriate name and Java type for the CMP field. Remember that only Java primitive types and Java serializable types are allowed.

The CMP field's accessor methods are now implemented. Right-click the field and choose Edit in Source View to examine the getter and setter method. Notice that by default the CMP field's accessor methods are defined in the entity bean's local interface, that is, are created with the @ejbgen:local-method tag.

To Change the CMP Field Properties

- To make a CMP field the primary key, right-click the CMP field and select Primary Key.
- To make a CMP field read-only, right-click the CMP field and select Read Only. Notice in Source View that the setter method no longer has the @ejbgen:local-method tag (or @ejbgen:remote-method tag if appropriate) set. In other words, the setter method is no longer defined in the bean's (local or remote) business interface.
- To change the definition of a CMP field's accessor methods in the Remote or Local interface, right-click the CMP field and use the Remote or Local options to change the settings. The accessor methods can be defined in either the Remote or Local interface, in both business interfaces or in neither interface.

Although not recommended as a best practice, it is possible to define a CMP field in the remote interface only. To do so, go to source view and delete the ejbgen:local-method tag for its getter and setter method.

Related Topics

How Do I: Add a Business Method to an Entity Bean?

Developing Enterprise JavaBeans