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.
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:
log
: This is the default setting. This option logs a message
for each orphaned key. It is an alias for the
org.apache.openjpa.event.LogOrphanedKeyAction
class, which has the following additional properties:
Channel
: The channel to log to. Defaults to
openjpa.Runtime
.
Level
: The level to log at. Defaults to WARN
.
exception
: Throw an
EntityNotFoundException
when OpenJPA discovers an
orphaned key. This is an alias for the
org.apache.openjpa.event.ExceptionOrphanedKeyAction
class.
none
: Ignore orphaned keys. This is an alias for the
org.apache.openjpa.event.NoneOrphanedKeyAction
class.
Example 7.26. Custom Logging Orphaned Keys
<property name="openjpa.OrphanedKeyAction" value="log(Channel=Orphans, Level=DEBUG)"/>