Specifying Bean Relationships in the EJB Jar Descriptor File
With CMP 2.0, you can define relationships between entity beans in the EJB jar descriptor file rather than the entity bean code. Note, though, that you can define such relationships only for local interfaces. For each relationship, the EJB jar descriptor must contain one
<ejb-relation> child element in the
<relationships> element.
The
<ejb-relation> element contains the following elements:
- <ejb-relation-name> (required). Relationship name.
- <ejb-relationship-role> (required). Relationship role. You should specify one of these elements for each entity in the relationship.
In turn, each
<ejb-relationship-role> element contains elements that describe the relationship itself:
- <ejb-relationship-role-name> (required). Role name of role.
- <relationship-role-source> (required). Source entity bean. You specify class name in the <ejb-name> element.
- <multiplicity> (required). Multiplicity of the <relationship-role-source>. Valid values are "One" and "Many".
- <cascade-delete>. Indicates that entities in this role should be deleted if an entity in the parent role (the role with multiplicity "One") is deleted.
- <cmr-field> (required). Container managed relationship field. You specify the field name with the <cmr-field-name> element, which should correspond to access methods in the entity bean class. You specify the field type with the <cmr-field-type> element.
JBlog's EJB jar descriptor contains one
element for the relationship Author-Story.
// The Author-Story relation as specified in
// the EJB jar descriptor
<ejb-relation>
<ejb-relation-name>Author-Story</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
author-stories
</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>Author</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>stories</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
stories-author
</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete/>
<relationship-role-source>
<ejb-name>Story</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>author</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>