EJB relationships

In the EJB tools, you can use the EJB deployment descriptor editor to optionally define a relationship between different enterprise beans.

Important: EJB 1.x relationships are WebSphere extensions, while EJB 2.x relationships are part of the EJB 2.x deployment descriptor. You open different wizards from different locations for the two levels of relationships.

There are three main types of relationship:

Relationship Description
one-to-one (1:1) A CMP entity bean is associated with a single instance of another CMP entity bean. For example, an Employee bean could be associated with only a single instance of an EmployeeID bean, because an employee can have only one employee identifier on file.
one-to-many (1:M) A CMP entity bean is associated with multiple instances of another CMP entity bean. For example, a Department bean could be associated with multiple instances of an Employee bean, because most departments are made up of more than one employee.
many-to-many (M:M) Multiple instances of a CMP entity bean are associated with multiple instances of another CMP entity bean. For example, multiple instances of a Customer bean could be associated with multiple instances of a Restaurant bean, because restaurants serve many customers, and customers patronize many different restaurants. For EJB 1.1 CMP entity beans, a many-to-many relationship requires the use of a link bean. This link bean is placed in the middle of the two enterprise beans by creating a 1:M relationship from each bean to the link bean. For EJB 2.0 CMP beans, the creation of the link bean is not required because the creation of the link table is handled automatically during the execution of a top-down mapping.

As part of defining a relationship, you assign a role to each bean relative to the other bean, and you give that role a name. For example, suppose you create a relationship between Employee and EmployeeID. The role of EmployeeID within the Employee bean could be something meaningful like id or employeeID. The role of Employee within the EmployeeID bean could be something like employee or owner. These names are used to derive method names in the generated code and become part of the enterprise bean's remote interface.

For EJB 2.x relationships, the cmr-field element is used to generate methods.

When CMP fields and relationships are eventually mapped to database tables, foreign keys are used to represent these relationships in the database tables. You can then add relationship roles to the key of an enterprise bean, which makes the foreign key represented in the relationship part of the key class.

In order to add a role to the key of a bean, it must be single multiplicity and it must be required (e.g., 1..1). Also, the role's owning bean must have the foreign-key check box selected. The rules are the same for EJB 2.x CMP entity beans except that the multiplicity will be 1 since there is no lower bound to test for requiredness. Note that the foreign-key check box is used to designate which CMP entity bean's mapped table should contain the foreign-key column. The CMP entity bean is the source enterprise bean for the role.

In the above example, the employeeID role can be made part of the Employee key if the multiplicity is 1..1, and Employee has the foreign-key check box selected. Also, marking a role as required (e.g., 1..x), will add the role's type as a parameter to the Home interface's create method. In EJB 2.x relationships, a cmr-field is required if the navigable check box is selected. You will only be able to select navigable for a CMP that has a local client view. Relationships can only be defined for the same EJB version of CMP entity beans. You cannot create a relationship between an EJB 1.x and an EJB 2.x CMP entity bean.

The EJB 2.x relationship creation wizard shows a UML view of the relationship and a specification view. Note that the multiplicity described in the specification view corresponds to the rules in the EJB specification. It is backwards to the UML view since the multiplicity for the role is relative to the relationship, and in the UML view it is relative to the source enterprise bean.

 

Related tasks

Defining bean relationships
Creating relationships for EJB 2.x beans