| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>11. Orphaned Keys</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.3 User's Guide"><link rel="up" href="ref_guide_mapping.html" title="Chapter 7. Mapping"><link rel="prev" href="ref_guide_mapping_custom.html" title="10. Custom Mappings"><link rel="next" href="ref_guide_deploy.html" title="Chapter 8. Deployment"></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">11. |
| Orphaned Keys |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ref_guide_mapping_custom.html">Prev</a> </td><th width="60%" align="center">Chapter 7. |
| Mapping |
| </th><td width="20%" align="right"> <a accesskey="n" href="ref_guide_deploy.html">Next</a></td></tr></table><hr></div><div class="section" title="11. Orphaned Keys"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="ref_guide_orphan">11. |
| Orphaned Keys |
| </h2></div></div></div> |
| |
| <p> |
| Unless you apply database foreign key constraints extensively, it is possible to |
| end up with orphaned keys in your database. For example, suppose <code class="classname"> |
| Magazine</code> <code class="literal">m</code> has a reference to <code class="classname">Article |
| </code><code class="literal">a</code>. If you delete <code class="literal">a</code> without |
| nulling <code class="literal">m</code>'s reference, <code class="literal">m</code>'s database |
| record will wind up with an orphaned key to the non-existent <code class="literal">a |
| </code> record. |
| </p> |
| <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> |
| <p> |
| One way of avoiding orphaned keys is to use <span class="emphasis"><em>dependent</em></span> |
| fields. |
| </p> |
| </div> |
| <p> |
| OpenJPA's <a class="link" href="ref_guide_conf_openjpa.html#openjpa.OrphanedKeyAction" title="5.53. openjpa.OrphanedKeyAction"><code class="literal"> |
| openjpa.OrphanedKeyAction</code></a> configuration property controls what |
| action to take when OpenJPA encounters an orphaned key. You can set this plugin |
| string (see <a class="xref" href="ref_guide_conf_plugins.html" title="4. Plugin Configuration">Section 4, “ |
| Plugin Configuration |
| ”</a>) to a custom |
| implementation of the |
| <a class="ulink" href="../javadoc/org/apache/openjpa/event/OrphanedKeyAction.html" target="_top"> |
| <code class="classname"> org.apache.openjpa.event.OrphanedKeyAction</code></a> |
| interface, or use one of the built-in options: |
| </p> |
| <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"> |
| <p> |
| <code class="literal">log</code>: This is the default setting. This option logs a message |
| for each orphaned key. It is an alias for the |
| <a class="ulink" href="../javadoc/org/apache/openjpa/event/LogOrphanedKeyAction.html" target="_top"> |
| <code class="classname">org.apache.openjpa.event.LogOrphanedKeyAction</code></a> |
| class, which has the following additional properties: |
| </p> |
| <div class="itemizedlist"><ul class="itemizedlist" type="circle"><li class="listitem"> |
| <p> |
| <code class="literal">Channel</code>: The channel to log to. Defaults to <code class="literal"> |
| openjpa.Runtime</code>. |
| </p> |
| </li><li class="listitem"> |
| <p> |
| <code class="literal">Level</code>: The level to log at. Defaults to <code class="literal">WARN |
| </code>. |
| </p> |
| </li></ul></div> |
| </li><li class="listitem"> |
| <p> |
| <code class="literal">exception</code>: Throw an <code class="classname"> |
| EntityNotFoundException</code> when OpenJPA discovers an |
| orphaned key. This is an alias for the |
| <a class="ulink" href="../javadoc/org/apache/openjpa/event/ExceptionOrphanedKeyAction.html" target="_top"> |
| <code class="classname">org.apache.openjpa.event.ExceptionOrphanedKeyAction</code> |
| </a> class. |
| </p> |
| </li><li class="listitem"> |
| <p> |
| <code class="literal">none</code>: Ignore orphaned keys. This is an alias for the |
| <a class="ulink" href="../javadoc/org/apache/openjpa/event/NoneOrphanedKeyAction.html" target="_top"> |
| <code class="classname">org.apache.openjpa.event.NoneOrphanedKeyAction</code></a> |
| class. |
| </p> |
| </li></ul></div> |
| <div class="example"><a name="ref_guide_orphan_logex"></a><p class="title"><b>Example 7.26. |
| Custom Logging Orphaned Keys |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| <property name="openjpa.OrphanedKeyAction" value="log(Channel=Orphans, Level=DEBUG)"/> |
| </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_mapping_custom.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ref_guide_mapping.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ref_guide_deploy.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">10. |
| Custom Mappings |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 8. |
| Deployment |
| </td></tr></table></div></body></html> |