blob: d158190edb2ab42f01c99a000d769fd2495ca250 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>Many-to-many mapping</title>
<meta name="generator" content="Oracle DARB XHTML Converter (Mode = ohj/ohw) - Version 5.1.1" />
<meta name="date" content="2010-05-19T8:12:44Z" />
<meta name="robots" content="noarchive" />
<meta name="doctitle" content="Many-to-many mapping" />
<meta name="relnum" content="Release 2.3" />
<meta name="copyright" content="Copyright (c) 2000, 2008 oracle . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html. Contributors: Oracle - initial API and implementation" />
<link rel="copyright" href="dcommon/html/cpyr.htm" title="Copyright" type="text/html" />
<link rel="stylesheet" href="dcommon/css/blafdoc.css" title="Oracle BLAFDoc" type="text/css" />
<!-- contents -->
</head>
<body>
<p><a id="BABEIEGD" name="BABEIEGD"></a></p>
<div class="sect2"><!-- infolevel="all" infotype="General" -->
<h1>Many-to-many mapping</h1>
<p><a id="sthref130" name="sthref130"></a><a id="sthref131" name="sthref131"></a><a id="sthref132" name="sthref132"></a><a id="sthref133" name="sthref133"></a>Use a <span class="bold">Many-to-Many Mapping</span> to define a many-valued association with many-to-many multiplicity. A many-to-many mapping has two sides: the <span class="italic">owning side</span> and <span class="italic">non-owning side</span>. You must specify the join table on the owning side. For bidirectional mappings, either side may be the owning side.</p>
<ol>
<li>
<p>In the <a href="ref_persistence_outline.htm#BABEGGFE">JPA Structure view</a>, select the field to map.</p>
</li>
<li>
<p>Right-click the field and then select <span class="bold">Map As &gt; Many-to-Many</span>. The <a href="ref_persistence_map_view.htm#BABIFBAF">JPA Details view (for attributes)</a> displays the properties for the selected field.</p>
</li>
<li>
<p>Use this table to complete the <a href="ref_mapping_general.htm#CACBHFIJ">General information</a> fields of the <span class="gui-object-title">JPA Details</span> view.</p>
<div class="inftblruleinformal">
<table class="RuleInformal" title="This table lists the fields in the Persistence Properties view for this mapping." summary="This table lists the fields in the Persistence Properties view for this mapping." dir="ltr" border="1" width="100%" frame="border" rules="all" cellpadding="3" cellspacing="0">
<col width="19%" />
<col width="*" />
<col width="41%" />
<thead>
<tr align="left" valign="top">
<th align="left" valign="bottom" id="r1c1-t19">Property</th>
<th align="left" valign="bottom" id="r1c2-t19">Description</th>
<th align="left" valign="bottom" id="r1c3-t19">Default</th>
</tr>
</thead>
<tbody>
<tr align="left" valign="top">
<td align="left" id="r2c1-t19" headers="r1c1-t19">Mapping Entity Hyperlink</td>
<td align="left" headers="r2c1-t19 r1c2-t19">Defines this mapping as a <span class="bold">Many to Many Mapping</span>.
<p>This field corresponds to the <code>@ManyToMany</code> annotation.</p>
</td>
<td align="left" headers="r2c1-t19 r1c3-t19">Many to Many</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r3c1-t19" headers="r1c1-t19">Target Entity</td>
<td align="left" headers="r3c1-t19 r1c2-t19">The entity to which this attribute is mapped.</td>
<td align="left" headers="r3c1-t19 r1c3-t19">null
<p>You do not need to explicitly specify the target entity, since it can be inferred from the type of object being referenced.</p>
</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r4c1-t19" headers="r1c1-t19">Fetch</td>
<td align="left" headers="r4c1-t19 r1c2-t19">Defines how data is loaded from the database. See <a href="ref_mapping_general.htm#CACGGGHB">"Fetch Type"</a> for details.
<ul>
<li>
<p>Eager</p>
</li>
<li>
<p>Lazy</p>
</li>
</ul>
</td>
<td align="left" headers="r4c1-t19 r1c3-t19">Lazy</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r5c1-t19" headers="r1c1-t19">Mapped By</td>
<td align="left" headers="r5c1-t19 r1c2-t19">The database field that owns the relationship.</td>
<td align="left" headers="r5c1-t19 r1c3-t19"><br /></td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r6c1-t19" headers="r1c1-t19">Order By</td>
<td align="left" headers="r6c1-t19 r1c2-t19">Specify the default order for objects returned from a query. See <a href="ref_mapping_general.htm#CACDADIH">"Order By"</a> for details.
<ul>
<li>
<p>No ordering</p>
</li>
<li>
<p>Primary key</p>
</li>
<li>
<p>Custom</p>
</li>
</ul>
</td>
<td align="left" headers="r6c1-t19 r1c3-t19">No ordering</td>
</tr>
</tbody>
</table>
<br /></div>
<!-- class="inftblruleinformal" --></li>
<li>
<p>Use this table to complete the fields in the <a href="reference011.htm#CACBAEBC">Join Table Information</a> area in the <span class="gui-object-title">JPA Details</span> view.</p>
<div class="inftblruleinformal">
<table class="RuleInformal" title="This table lists the fields in the Join Table tab of the Persistence Properties view for this mapping." summary="This table lists the fields in the Join Table tab of the Persistence Properties view for this mapping." dir="ltr" border="1" width="100%" frame="border" rules="all" cellpadding="3" cellspacing="0">
<col width="19%" />
<col width="*" />
<col width="38%" />
<thead>
<tr align="left" valign="top">
<th align="left" valign="bottom" id="r1c1-t20">Property</th>
<th align="left" valign="bottom" id="r1c2-t20">Description</th>
<th align="left" valign="bottom" id="r1c3-t20">Default</th>
</tr>
</thead>
<tbody>
<tr align="left" valign="top">
<td align="left" id="r2c1-t20" headers="r1c1-t20">Name</td>
<td align="left" headers="r2c1-t20 r1c2-t20">Name of the join table that contains the foreign key column.</td>
<td align="left" headers="r2c1-t20 r1c3-t20">You must specify the join table on the owning side.
<p>By default, the name is assumed to be the primary tables associated with the entities concatenated with an underscore.</p>
</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r3c1-t20" headers="r1c1-t20">Join Columns</td>
<td align="left" headers="r3c1-t20 r1c2-t20">Select <span class="bold">Override Default</span>, then Add, Edit, or Remove the join columns.</td>
<td align="left" headers="r3c1-t20 r1c3-t20">By default, the name is assumed to be the primary tables associated with the entities concatenated with an underscore.</td>
</tr>
<tr align="left" valign="top">
<td align="left" id="r4c1-t20" headers="r1c1-t20">Inverse Join Columns</td>
<td align="left" headers="r4c1-t20 r1c2-t20">Select <span class="bold">Override Default</span>, then Add, Edit, or Remove the join columns.</td>
<td align="left" headers="r4c1-t20 r1c3-t20">By default, the mapping is assumed to have a single join.</td>
</tr>
</tbody>
</table>
<br /></div>
<!-- class="inftblruleinformal" --></li>
<li>
<p>To add a new Join or Inverse Join Column, click <span class="gui-object-action">Add</span>.</p>
<p>To edit an existing Join or Inverse Join Column, select the field to and click <span class="gui-object-action">Edit</span>.</p>
</li>
</ol>
<p>Eclipse adds the following annotations to the field:</p>
<pre xml:space="preserve" class="oac_no_warn">
@JoinTable(joinColumns=@JoinColumn(name="<span class="italic">&lt;JOIN_COLUMN&gt;</span>"),
name = "<span class="italic">&lt;JOIN_TABLE_NAME&gt;</span>")
@ManyToMany(cascade=CascadeType.<span class="italic">&lt;CASCADE_TYPE&gt;</span>, fetch=FetchType.<span class="italic">&lt;FETCH_TYPE&gt;</span>,
targetEntity=<span class="italic">&lt;TARGET_ENTITY&gt;</span>, mappedBy = "<span class="italic">&lt;MAPPED_BY&gt;</span>")
@OrderBy("<span class="italic">&lt;ORDER_BY&gt;</span>")
</pre>
<p>&nbsp;</p>
<img src="img/ngrelt.png" alt="Related task" title="Related task" /><br />
<br />
<a href="task_mapping.htm#BABDGBIJ">Mapping an entity</a>
<p>&nbsp;</p>
<img src="img/ngrelr.png" alt="Related reference" title="Related reference" /><br />
<br />
<a href="ref_persistence_outline.htm#BABEGGFE">JPA Structure view</a><br />
<a href="ref_persistence_map_view.htm#BABIFBAF">JPA Details view (for attributes)</a>
<p>&nbsp;</p>
<img src="img/ngrelc.png" alt="Related concept" title="Related concept" /><br />
<br />
<a href="concept_mapping.htm#BABBDJFI">Understanding OR mappings</a><br />
<a href="concepts001.htm#BABBGFJG">Understanding EJB 3.0 Java Persistence API</a></div>
<!-- class="sect2" -->
<!-- Start Footer -->
<div class="footer">
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<col width="86%" />
<col width="*" />
<tr>
<td align="left"><span class="copyrightlogo">Copyright&nbsp;&copy;&nbsp;2006, 2010,&nbsp;Oracle&nbsp;and/or&nbsp;its&nbsp;affiliates.&nbsp;All&nbsp;rights&nbsp;reserved.</span><br />
<a href="dcommon/html/cpyr.htm"><span class="copyrightlogo">Legal Notices</span></a></td>
</tr>
</table>
</div>
<!-- class="footer" -->
</body>
</html>