Annotation Type OnDelete


  • @Target({METHOD,FIELD,TYPE})
    @Retention(RUNTIME)
    public @interface OnDelete
    Specifies an on delete action for a foreign key constraint. The most common usage is @OnDelete(action = CASCADE).
     @ManyToOne
     @OnDelete(action = CASCADE)
     Parent parent;
     
    Note that this results in an on delete cascade clause in the DDL definition of the foreign key. It's completely different to CascadeType.REMOVE.

    In fact, @OnDelete may be combined with cascade=REMOVE.

     @ManyToOne(cascade = REMOVE)
     @OnDelete(action = CASCADE)
     Parent parent;
     
    • If @OnDelete(action = CASCADE) is used in conjunction with cascade=REMOVE, then associated entities are fetched from the database, marked deleted in the persistence context, and evicted from the second-level cache.
    • If @OnDelete(action = CASCADE) is used on its own, without cascade=REMOVE, then associated entities are not fetched from the database, are not marked deleted in the persistence context, and are not automatically evicted from the second-level cache.

    Like database triggers, on delete actions can cause state held in memory to lose synchronization with the database.

    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      OnDeleteAction action
      The action to taken by the database when deletion of a row would cause the constraint to be violated.
    • Element Detail

      • action

        OnDeleteAction action
        The action to taken by the database when deletion of a row would cause the constraint to be violated.