Annotation Type DbForeignKey


@Retention(RUNTIME) @Target(FIELD) public @interface DbForeignKey
Specify how a foreign key constraint should be defined.

We can specify if a constraint should not be defined at all or control the onDelete and onUpdate modes used.

Example: On delete cascade



 @DbForeignKey(onDelete = ConstraintMode.CASCADE)
 @ManyToOne
 RelatedBean parent;

 

Example: No foreign key



 // No FK Constraint
 @DbForeignKey(noConstraint=true)
 @ManyToOne
 RelatedBean parent;

 

Example: On delete set null



 @DbForeignKey(onDelete = ConstraintMode.SET_NULL)
 @ManyToOne
 RelatedBean parent;

 
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Set to true when we do not wish any foreign key constraint to be created.
    boolean
    Set to true when we do not wish an index to be created on the foreign key column(s).
    Specify the onDelete mode.
    Do NOT change this - seriously, don't do it.
  • Element Details

    • onDelete

      ConstraintMode onDelete
      Specify the onDelete mode. Typically will default to RESTRICT (aka No Action).
      Default:
      RESTRICT
    • onUpdate

      ConstraintMode onUpdate
      Do NOT change this - seriously, don't do it.

      Your primary keys should never change by design. We should orientate the design to support primary keys that change (and instead find better non mutating primary keys). Oracle go to the point of actively not supporting "on update" for this reason.

      So yes, we can specify the onUpdate mode but I don't expect anyone to change this.

      Default:
      RESTRICT
    • noConstraint

      boolean noConstraint
      Set to true when we do not wish any foreign key constraint to be created. When this is set to true the onDelete and onUpdate have no effect.
      Default:
      false
    • noIndex

      boolean noIndex
      Set to true when we do not wish an index to be created on the foreign key column(s).
      Default:
      false