7.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.


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

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

Example 7.35. Custom Logging Orphaned Keys

JPA XML format:

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

JDO properties format:

kodo.OrphanedKeyAction: log(Channel=Orphans, Level=DEBUG)