JPA CascadeType.REMOVE vs Hibernate @OnDelete

By , 13 June 2014

JPA CascadeType.REMOVE vs Hibernate @OnDelete
JPA CascadeType.REMOVE vs Hibernate @OnDelete

Somehow database models and ORM always end up being more difficult than you expect. Here is a common source of confusion between JPA cascade operations and database cascade operations. Basically they do the opposite thing. For example:

public class House {

    @OneToOne
    Object door;
}

If you use CascadeType.REMOVE then deleting the house will also delete the door (using an extra SQL statement).

    @OneToOne(cascade=CascadeType.REMOVE)
    Object door;

If you use @OnDelete then deleting the door will also delete the house (using an ON DELETE CASCADE database foreign key).

    @OneToOne
    @OnDelete(action = OnDeleteAction.CASCADE)
    Object door;

JPA has not standardized the ON DELETE and ON UPDATE foreign key actions possibly because they are SQL-specific and JPA is supposed to be storage-agnostic. I think this is unfortunate - what I'm looking for is ON DELETE SET NULL which would mean that when I delete the door, House.door gets set to null automatically. It's a fairly common requirement and is implemented in OpenJPA like this:

    @OneToOne
    @ForeignKey(deleteAction=ForeignKeyAction.NULL)
    Object door;

For the moment it looks like I'll have to stick to OpenJPA. Not sure why this isn't an option in Hibernate.

JPA CascadeType.REMOVE vs Hibernate @OnDelete

About I Like Spam

JPA CascadeType.REMOVE vs Hibernate @OnDelete

Leave a Comment

Please visit https://ilikespam.com/jpa-cascadetype-remove-vs-hibernate-ondelete to add your comments.