JPA / Hibernate Cascade.Remove用例@ManyToOne [英] JPA / Hibernate Cascade.Remove use case for @ManyToOne

查看:92
本文介绍了JPA / Hibernate Cascade.Remove用例@ManyToOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下内容:

With the following:

//User class...

@ManyToOne(cascade=CascadeType.REMOVE)
@JoinColumn(name="INSTITUTION_ID")
public void setInstitution(final Institution institution) {
    this.institution = institution;
}

这是否意味着删除User对象会删除与之关联的Institution对象?如果是这种情况,我不会跟踪为什么会这样。比如说,该机构有许多用户。这是否意味着删除其中一个用户会删除该机构,在这种情况下,所有其他用户都会失去它?

Does this mean deleting a User object would remove the Institution object associated with it? If that is the case I don't follow why this would be needed. Say for example that the institution had many users. Does this mean that deleting one of those Users deletes the institution, in which case all the other Users lose it aswell?

推荐答案

您是正确的,它很少(如果有的话)需要,而不是可移植的构造与ManyToMany或ManyToOne一起使用CascadeType.REMOVE。这在JPA 2.0规范中有如下记载:

You are right, it is rarely (if ever) needed and not portable construct to use CascadeType.REMOVE with ManyToMany or ManyToOne . This is documented as follows in JPA 2.0 specification:


关系建模注释约束了
cascade = REMOVE规范的使用。 cascade = REMOVE规范应该仅适用于指定为OneToOne或
OneToMany的关联。将cascade = REMOVE应用于其他
关联的应用程序不可移植。

The relationship modeling annotation constrains the use of the cascade=REMOVE specification. The cascade=REMOVE specification should only be applied to associations that are specified as OneToOne or OneToMany. Applications that apply cascade=REMOVE to other associations are not portable.

存在的原因可能仅仅是存在没有合理的方法来限制CascadeType枚举的某些子集的级联值。

Reason why it exists is likely just that there was no reasonable way to limit allowed values of cascade to some subset of CascadeType enumeration's values.

这篇关于JPA / Hibernate Cascade.Remove用例@ManyToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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