JPA的cascade = REMOVE和Hibernate的@OnDelete一起使用? [英] JPA's cascade = REMOVE and Hibernate's @OnDelete used together?

查看:767
本文介绍了JPA的cascade = REMOVE和Hibernate的@OnDelete一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了几乎所有关系都具有以下注释的代码库:

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

现在我无法理解 @OnDelete 是否首先执行的操作。 Hibernate:OnDelete vs cascade = CascadeType.REMOVE 很有趣,但不幸的是,没有任何答案,并且 @OnDelete 的JavaDoc特别没有价值。



从其他问题看,它看起来像OnDelete注释以某种方式让数据库执行级联,而 @OneToMany 上的级联指令让ORM执行它,但是将它们一起使用的目的是什么? p>

并且 @OneToMany 的cascade指令真的不允许ORM实现生成基于数据库的级联呢?

解决方案

假设您有一对一的定向关系

  class House {

@OneToOne
对象门;






如果你使用CascadeType.REMOVE,那么删除房子也会删除大门。

  @OneToOne(cascade = CascadeType.REMOVE)
对象门;

如果您使用@O​​nDelete,那么删除大门也会删除房屋。

  @OneToOne 
@OnDelete(action = OnDeleteAction.CASCADE)
对象门;

在这里阅读更多: http://www.ninthavenue.com.au/jpa-cascadetype-remove-vs-hibernate-ondelete


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?

解决方案

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

这篇关于JPA的cascade = REMOVE和Hibernate的@OnDelete一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆