Creating entity beans with container-managed persistence

You can use the Create an Enterprise Bean wizard to create a container-managed persistence (CMP) entity bean.

To create an entity bean with container-managed persistence (CMP):

  1. In the J2EE perspective, click File > New > Other > EJB > Enterprise Bean. The Create an Enterprise Bean wizard appears.
  2. Select the Entity bean with container-managed persistence (CMP) fields radio button.
  3. Select the EJB Component that you want to add the bean to.
  4. In the Bean name field, type the name that you want to assign to the enterprise bean. By convention, bean names should begin with an uppercase letter.
    Note: You can use Unicode characters for the bean name, but Unicode characters are not supported for enterprise bean packages and classes associated with enterprise beans.
  5. In the Source folder field, select the source folder for the new bean.
  6. In the Default package field, enter the package name for the new bean.
  7. If you are adding the bean to an EJB 2.0 or later project, in the CMP version drop-down list, select the EJB specification level that you want to use for the new entity bean.
  8. Optional: For EJB 2.x beans, you can select Generated an annotated bean class. If you select this option, the wizard generates annotations at the beginning of the Java™ code for the bean class. The annotations define the bean's implementation as you specify in the wizard. The annotations are then used by the EJB tools to generate the necessary bean classes, and to provide values that are inserted in the EJB deployment descriptor (ejb-jar.xml). Select this option if you are familiar with annotations and want to use the annotations to update the bean rather than using the deployment descriptor.

    For more information, see Annotation-based programming overview.

  9. Click Next.
  10. In the Bean class field, enter the desired package and class name for the bean class. By default, the wizard suggests a bean class based on the bean name and default package that you defined. A bean class can be a new class that the wizard generates, or it can be an existing class in the project class path. Click the Class button to open a dialog that lists the classes in the project that correspond to the bean type. The name of the bean class appears blue for existing classes with source. The name of the bean class appears red for existing binary classes.
  11. Define the client views and interfaces. For EJB 2.0 or later beans, you can include a remote client view, a local client view, or both. For EJB 1.1 beans, only a remote client view is supported. Every session or entity bean must have at least one client view:
    • Remote client view: Select this check box to include a remote client view for the session bean.

      In the Remote home interface and Remote interface fields, enter the package and class names that you want to use for the remote client view interfaces. The wizard uses the bean name and default package to suggest values for the interface package and class names.

    • Local client view: Select this check box to include a local client view for the session bean.

      In the Local home interface and Local interface fields, enter the package and class names that you want to use for the local client view interfaces. The wizard uses the bean name and default package to suggest values for the interface package and class names.

  12. Optional: In the Key class field, enter the package and class name for the bean's key class. By default, the Use the single key attribute type for the key class check box is selected, and the type of the key CMP attribute is used as the key class. Use this option if you have a single key attribute whose type is a valid primary key field (for example, java.lang.Integer). If you include more than one key attribute, a new compound key class is created. To specify a different key class, deselect the Use the single key attribute type for the key class check box, and provide the name of the key class.
  13. Optional: Define the CMP attributes for the entity bean.
    1. Click the Add button.
    2. Specify the Name and Type for the attribute.
    3. Optional: If the attribute is an array, select the Array check box. Specify the number of Dimensions in the array.
    4. Optional: Select Key field to make the attribute a key field for the entity bean.
    5. Optional: Depending on the client views in your new bean, you can choose to promote the getter and setter methods for the attribute to the remote and local interfaces.
    6. Click Apply to create the attribute. Click Close when you are finished defining attributes.
    By default, the wizard specifies the following CMP attribute and declares it the key field:
    • Name: id
    • Type: java.lang.Integer
    You can edit or remove this attribute.
    Note: A key is required if the bean is not inherited. If a key is not defined, the bean will have validation errors. After you create the bean, you can use the deployment descriptor editor to add a key attribute or make an existing attribute the key.
  14. Click Next.
  15. Optional: In the Bean superclass field, type or select the desired class.
  16. Optional: Define any interfaces that you want the remote or local client interfaces to extend:
    • Click the Add button to open the Type Selection dialog box where you can select the interface that you want to extend.
    • If you added an interface that you no longer want to extend, select the interface in the list and click Remove.
  17. Click Finish. The new entity bean is added to the specified EJB project.
Restriction: When setting up inherited CMP entity beans, children beans cannot have CMP attributes with the same name as those defined anywhere in their ancestry chains. For example, Parent is a CMP entity bean that contains an attribute id of type int. If you create a CMP bean called Child using the wizard and specified Parent as its super type and attempted to add an attribute id of type int, the id attribute would not be added because it would inherit Parent's id attribute. If Child2, another unrelated CMP Bean, contained an attribute id of type java.lang.String and had its inheritance structure changed through the Deployment Descriptor Editor to inherit from Parent, Child2's id attribute of type java.lang.String would be removed because it would inherit Parent's id attribute and there may be validation errors due to type conflicts.

Feedback