Creating relationships for EJB 1.x beans

Relationships for EJB 1.x beans are not standard to the EJB 1.x specification. WebSphere Application Server extensions were created to handle 1.x relationships.

Use the EJB deployment descriptor editor to specify the relationship between two enterprise beans in your EJB project or module. The EJB deployment descriptor editor generates the appropriate finder methods to support any relationships that you create.

When you define a relationship from one CMP to another, extra CMP fields based on the key fields from the referencing CMP bean are added to the owning CMP bean of the forward referencing role (Foreign Key check box is selected on the owning CMP bean).

For example, Company has a 1..N relationship to Employee with roles company and employees respectively. The company role is forward and the Company CMP bean has key fields named "companyID" and "companyName." The CMP fields that are automatically added to the Employee bean are "company_companyID" and "company_companyName." These fields are hidden in the EJB editor because they are fields that were added to support the "employee" relationship role. Problems would arise if these CMP fields were removed from the ejb-jar.xml file while the relationship still existed.

More examples of relationships can be seen in the AutoWorld example project that is shipped with Rational Application Developer. The picture below shows how the Garage-Vehicle relationship appears on the Relationships page of the EJB deployment descriptor editor. Multiple Vehicles can be housed in a single Garage, so the multiplicity indicates a Many (0..*) to 1 (0..1) relationship.


Screen capture of the Add Relationship wizard.

The creation wizard displays the UML view of the relationship and the fields are modified based on the UML model. When a relationship is created, additional classes are created to support the runtime in maintaining the links between these two beans. Also, additional methods will be generated into the Home, Remote, and Bean classes of both enterprise beans. You will see compile errors in the EJB deployment descriptor editor when a relationship is first created because the generated Link classes need to be compiled. You will see the actual code in these files but the changes in the Homes, Remotes, and Bean classes have not yet been compiled. This will not take place until the project is built; this usually happens automatically when the EJB deployment descriptor editor is saved. Therefore, you will not see compile errors until the editor is saved.

Note: If a relationship exists between one or more CMP beans, changing the key shape of one CMP may result in the related CMPs being regenerated.

To create a relationship between two enterprise beans (EJB 1.x):

  1. Switch to the J2EE perspective.

  2. In the J2EE Hierarchy view, select the EJB module that includes enterprise beans that you want to associate with each other.

  3. Select Open With > Deployment Descriptor Editor from the pop-up menu.

  4. On the Overview page of the editor, select the Relationships section.

  5. Click Add. The Add Relationship wizard appears.

  6. Select one enterprise bean from the Bean A list (left column).

  7. Select one enterprise bean from the Bean B list (right column). The Relationship name field is automatically created based on the names of the enterprise beans chosen. However, you can edit this field to rename the relationship.

  8. Click Next.

  9. By default, Role names are automatically generated, but can be modified.

  10. Set parameters for each role. Choose from the following options:

    • Navigable means that instances of the other bean in the association can be retrieved using this role.

    • Multiplicity means that the role potentially represents many instances of the other bean. Using 1..x makes the role required.

    • Foreign Key means that the specified enterprise bean of the relationship holds a foreign key for the other relationship role.

  11. Click Finish.

Note: In order to support EJB 1.1 CMP relationships, Link classes are created. These Link classes require the knowledge of the beans primary key classes. If you change the primary key class for a 1.1 CMP that is involved in relationships, the generated Link classes will still contain references to the old primary key class. The workaround is to manually update the Link classes. There should only be two occurrences where the change is necessary.