11.  Orphaned Keys

Unless you apply database foreign key constraints extensively, it is possible to end up with orphaned keys in your database. For example, suppose Magazine m has a reference to Article a. If you delete a without nulling m's reference, m's database record will wind up with an orphaned key to the non-existent a record.

Note

One way of avoiding orphaned keys is to use dependent fields.

OpenJPA's openjpa.OrphanedKeyAction configuration property controls what action to take when OpenJPA encounters an orphaned key. You can set this plugin string (see Section 4, “ Plugin Configuration ”) to a custom implementation of the org.apache.openjpa.event.OrphanedKeyAction interface, or use one of the built-in options:

Example 7.26.  Custom Logging Orphaned Keys

<property name="openjpa.OrphanedKeyAction" value="log(Channel=Orphans, Level=DEBUG)"/>