| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>5. Managed Inverses</title><link rel="stylesheet" href="css/docbook.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.72.0"><link rel="start" href="manual.html" title="Apache OpenJPA User's Guide"><link rel="up" href="ref_guide_pc.html" title="Chapter 5. Persistent Classes"><link rel="prev" href="ref_guide_pc_oid.html" title="4. Object Identity"><link rel="next" href="ref_guide_pc_scos.html" title="6. Persistent Fields"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5. |
| Managed Inverses |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ref_guide_pc_oid.html">Prev</a> </td><th width="60%" align="center">Chapter 5. |
| Persistent Classes |
| </th><td width="20%" align="right"> <a accesskey="n" href="ref_guide_pc_scos.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ref_guide_inverses"></a>5. |
| Managed Inverses |
| </h2></div></div></div><a class="indexterm" name="d0e22482"></a><p> |
| Bidirectional relations are an essential part of data modeling. |
| <a href="jpa_overview_mapping.html" title="Chapter 12. Mapping Metadata">Chapter 12, <i xmlns:xlink="http://www.w3.org/1999/xlink"> |
| Mapping Metadata |
| </i></a> in the JPA Overview explains how to |
| use the <code class="literal">mappedBy</code> annotation attribute to form bidirectional |
| relations that also share datastore storage in JPA. |
| </p><p> |
| OpenJPA also allows you to define purely logical bidirectional relations. The |
| <a xmlns:xlink="http://www.w3.org/1999/xlink" href="../javadoc/org/apache/openjpa/persistence/InverseLogical.html" target="_top"> |
| <code class="classname">org.apache.openjpa.persistence.InverseLogical</code></a> |
| annotation names a logical inverse in JPA metadata. |
| </p><div class="example"><a name="ref_guide_inverses_logicalex"></a><p class="title"><b>Example 5.8. |
| Specifying Logical Inverses |
| </b></p><div class="example-contents"><p> |
| <code class="literal">Magazine.coverPhoto</code> and <code class="literal">Photograph.mag</code> are |
| each mapped to different foreign keys in their respective tables, but form a |
| logical bidirectional relation. Only one of the fields needs to declare the |
| other as its logical inverse, though it is not an error to set the logical |
| inverse of both fields. |
| </p><pre class="programlisting"> |
| import org.apache.openjpa.persistence.*; |
| |
| @Entity |
| public class Magazine { |
| |
| @OneToOne |
| private Photograph coverPhoto; |
| |
| ... |
| } |
| |
| @Entity |
| public class Photograph { |
| |
| @OneToOne |
| @InverseLogical("coverPhoto") |
| private Magazine mag; |
| |
| ... |
| } |
| </pre></div></div><br class="example-break"><p> |
| Java does not provide any native facilities to ensure that both sides of a |
| bidirectional relation remain consistent. Whenever you set one side of the |
| relation, you must manually set the other side as well. |
| </p><p> |
| By default, OpenJPA behaves the same way. OpenJPA does not automatically |
| propagate changes from one field in bidirectional relation to the other field. |
| This is in keeping with the philosophy of transparency, and also provides higher |
| performance, as OpenJPA does not need to analyze your object graph to correct |
| inconsistent relations. |
| </p><p> |
| <a class="indexterm" name="d0e22520"></a> |
| If convenience is more important to you than strict transparency, however, you |
| can enable inverse relation management in OpenJPA. Set the |
| <a href="ref_guide_conf_openjpa.html#openjpa.InverseManager" title="5.35. openjpa.InverseManager"><code class="classname">openjpa.InverseManager |
| </code></a> plugin property to <code class="literal">true</code> for standard |
| management. Under this setting, OpenJPA detects changes to either side of a |
| bidirectional relation (logical or physical), and automatically sets the other |
| side appropriately on flush. |
| </p><div class="example"><a name="ref_guide_inversesex"></a><p class="title"><b>Example 5.9. |
| Enabling Managed Inverses |
| </b></p><div class="example-contents"><pre class="programlisting"> |
| <property name="openjpa.InverseManager" value="true"/> |
| </pre></div></div><br class="example-break"><p> |
| The inverse manager has options to log a warning or throw an exception when it |
| detects an inconsistent bidirectional relation, rather than correcting it. To |
| use these modes, set the manager's <code class="literal">Action</code> property to |
| <code class="literal">warn</code> or <code class="literal">exception</code>, respectively. |
| </p><p> |
| By default, OpenJPA excludes <a href="ref_guide_pc_scos.html#ref_guide_pc_scos_proxy_lrs" title="6.4.2. Large Result Set Proxies"> large |
| result set fields</a> from management. You can force large result set fields |
| to be included by setting the <code class="literal">ManageLRS</code> plugin property to |
| <code class="literal">true</code>. |
| </p><div class="example"><a name="ref_guide_inverses_logex"></a><p class="title"><b>Example 5.10. |
| Log Inconsistencies |
| </b></p><div class="example-contents"><pre class="programlisting"> |
| <property name="openjpa.InverseManager" value="true(Action=warn)"/> |
| </pre></div></div><br class="example-break"></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ref_guide_pc_oid.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ref_guide_pc.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ref_guide_pc_scos.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4. |
| Object Identity |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 6. |
| Persistent Fields |
| </td></tr></table></div></body></html> |