Search code examples
hibernatejpaforeign-keys

Force Hibernate to create foreign key with ON DELETE CASCADE


When Hibernate (or another JPA implementation) creates foreign key for @OneToMany relationship, is there a way to force it to use ON DELETE CASCADE at the database level? I found that when I use CascadeType.DELETE, Hibernate doesn't do it at the database level, but sends two delete statements (for parent and child records) instead. Or maybe there's a good reason for that?


Solution

  • In hibernate, you can use

    @OnDelete(action = OnDeleteAction.CASCADE)
    

    on your @OneToMany relationship. This tells hibernate to set ON DELETE CASCADE for the generated foreign key.

    Note that this is a hibernate extension and is not specified in the JPA standard.

    Use this with caution. When you let database cascade the deletes, these happen outside the control of hibernate so:

    • Your second-level cache might become out of sync.
    • You cant use on delete listeners on those entities.

    I think you should only use this if you have a large collection and performance consideration forces you to let database handles the delete instead of hibernate.