Java Reference
In-Depth Information
<role-source>
<ejb-name>YachtEJB</ejb-name>
</role-source>
<cmr-field>
<cmr-field-name>engine</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>Engine-In-Yacht</ejb-relationship-role-
name>
<multiplicity>one</multiplicity>
<role-source>
<ejb-name>EngineEJB</ejb-name>
</role-source>
</ejb-relationship-role>
</ejb-relation>
This part of the deployment descriptor tells the EJB container that each
YachtEJB
has one engine and
each
EngineEJB
may belong to a yacht. Thus, this is a one-to-one relationship. Since you can only find
the
EngineEJB
instance through a
YachtEJB
instance, not vice verse, this relationship is
unidirectional. Although similar to the primary-key and foreign-key relationships in a database, the EJB
relationships do not work the same way as relationships in database. An EJB relationship binds two
EJBs together through object graphs to have in-memory object graphs mapped to an underlying
database schema. For example, the
YachtEJB
owns an
EngineEJB;
thus, when a
YachtEJB
instance is instantiated, an associated
EngineEJB
instance must also be instantiated and placed in the
EJB container's memory for a client to access. In other words, the EJB relationship is enforced and
adhered to by the EJBs and the EJB container.
Cardinality and Direction of Relationship
The XML elements used in the deployment descriptor to describe the container managed can become
very complex, as they must deal with both the
cardinality
and direction (unidirectional vs. bidirectional)
of the relationships.
Cardinality indicates the number of EJBs. The four types of multiplicities are as follows:
One-to-one
: Each entity-bean instance is related to a single instance of another entity bean. For
example, if each yacht has only one engine,
YachtEJB
and
EngineEJB
will have a one-to-one
relationship.
One-to-many
: An entity-bean instance may be related to multiple instances of the other entity
bean. In real life, yachts have twin engines, and some have even more engines. To reflect this fact,
the
YachtEJB
has a one-to-many relationship with
EngineEJB
.
Many-to-one
: Multiple instances of an entity bean may be related to a single instance of the other
entity bean. This multiplicity is the opposite of a one-to-many relationship. In the example
mentioned in the previous item, from the perspective of
EngineEJB
the relationship to
YachtEJB
is many-to-one.
Many-to-many
: The entity-bean instances may be related to multiple instances of each other. For
example, in college, each course has many students, and every student may take several courses.
Therefore, in an enrollment application,
CourseEJB
and
StudentEJB
have a many-to-many
relationship.
The direction of a relationship may be either bidirectional or unidirectional. In a
bidirectional
relationship,
each entity bean has a relationship field that refers to the other bean. Through the relationship field, an
entity bean's code can access its related object. If an entity bean has a relative field, we often say that it