blob: bd592c7cab61fe20435e374f4f188d3f73b13026 [file] [log] [blame]
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<link rel="stylesheet" type="text/css" href="../../org.eclipse.wst.doc.user/common.css" />
<title>EJB relationships</title>
</head>
<body id="ceassoc"><a name="ceassoc"><!-- --></a>
<h1 class="topictitle1">EJB relationships</h1>
<div><p>In the EJB tools, you can use the EJB deployment descriptor editor
to optionally define a relationship between different enterprise beans.</p><div class="important"><span class="importanttitle">Important: </span>EJB 1.x relationships are WebSphere<sup>®</sup> 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.</div>
<p>There are three main types of relationship:</p>
<ul><li>one-to-one</li>
<li>one-to-many</li>
<li>and many-to-many</li>
</ul>
<p>In a one-to-one (1:1) relationship, 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.</p>
<p>In a one-to-many (1:M) relationship, 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.</p>
<p>In a many-to-many (M:M) relationship, 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.</p>
<p>As part of defining a relationship, you assign a <i>role</i> 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 <i>id</i> or <i>employeeID</i>.
The role of Employee within the EmployeeID bean could be something like <i>employee</i> or <i>owner</i>.
These names are used to derive method names in the generated code and become
part of the enterprise bean's remote interface.</p>
<p>For EJB 2.x relationships, the cmr-field element is used to generate methods.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
</div>
<div><p><b class="reltaskshd">Related tasks</b><br />
<a href="../topics/teassoc.html" title="Use the EJB deployment descriptor editor to specify the relationship between two enterprise beans in your EJB module.">Defining bean relationships</a><br />
<a href="../topics/tereltwo.html" title="Use the Add Relationship wizard to specify the relationship between two enterprise beans in your EJB project or module. The appropriate finder methods are generated to support any relationships that you create.">Creating relationships for EJB 2.x beans</a><br />
</p>
</div> <br />
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.
</body>
</html>