Adding relationship roles to keys
You can add a relationship role to a key class.
The relationship role to be added must have the following characteristics:
- It is not many-valued.
- It is both required and navigable.
- It holds a foreign key.
Additionally, the enterprise bean to which the key will be added must be a root bean (does not inherit from a supertype). This is because inherited beans must use the primary key class of the supertype and therefore the key class shape cannot be modified.
The following task refers to two enterprise beans: one that contains a role (this bean) and one represented in the role (the other bean).
- In the Project Explorer view of the J2EE perspective, right-click the Deployment Descriptor for your EJB project and select Open With > Deployment Descriptor Editor to open the deployment descriptor editor.
- On the Bean page, select the enterprise bean that contains the relationship role that you want to add to the key, and scroll to the Relationships section.
- Click Add to Key.
This action adds the key fields of the other bean to both the key class and the bean class of this bean. When a role is added to the primary key, the association is also regenerated.
Be careful when removing beans from an association. If you have added one of the roles to the key of one of the beans, the bean with the role in the key must be removed before the other bean. A database constraint may enforce this (even if the role is not part of the key). However, if there is no database constraint, problems could still occur. For example, suppose that you have Customer and Address beans in a 1:1 association where the customer role of the Address bean holds the foreign key and the customer role is the primary key of the Address bean. If you were to remove the Customer bean without first removing the Address bean, the association maintenance code, while trying to set the Address bean's customer role foreign-key to null, will also be trying to set the primary key of the Address bean to null.
Related tasks
Adding a security identity (bean level)
Defining security roles for EJB modules
Adding security role references