JPA's cascade = REMOVE and Hibernate's @OnDelete used together?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 01:19:11

问题


I have inherited a code base on which nearly all relations have the following annotations:

@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "someThing")
@OnDelete(action = OnDeleteAction.CASCADE)

Now I'm having trouble understanding what @OnDelete does in the first place. Hibernate: OnDelete vs cascade=CascadeType.REMOVE is interesting, but unfortunately doesn't have any answers and the JavaDoc for @OnDelete is particularly worthless.

From the other questions it looks like the OnDelete annotation somehow lets the DB do the cascading, while the cascading directive on @OneToMany let's the ORM do it, but what would ever be the purpose of using them together?

And does @OneToMany's cascade directive really doesn't allow the ORM implementation to generate a DB based cascade anyway?


回答1:


Let's say you have a one-to-one directional relationship

class House {

    @OneToOne
    Object door;

}

If you use CascadeType.REMOVE then deleting the house will also delete the door.

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

If you use @OnDelete then deleting the door will also delete the house.

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

Read more here: http://www.ninthavenue.com.au/jpa-cascadetype-remove-vs-hibernate-ondelete



来源:https://stackoverflow.com/questions/8563592/jpas-cascade-remove-and-hibernates-ondelete-used-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!