3.8.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 Magazinem has a reference to Articlea.
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 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 toopenjpa.Runtime. -
Level: The level to log at. Defaults toWARN.
-
-
exception: Throw anEntityNotFoundExceptionwhen 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.
<property name="openjpa.OrphanedKeyAction" value="log(Channel=Orphans, Level=DEBUG)"/>