在JPA级联的一个问题 [英] A question on JPA Cascading

查看:190
本文介绍了在JPA级联的一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体称为User和用户配置在我的数据模型。这里是他们是如何映射。

I have two entities called User and UserProfile in my datamodel. Here is how they are mapped.

从用户实体code:

@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
public UserProfile getUserProfile(){
    return this.userProfile;
}

public void setUserProfile(UserProfile userProfile){
    this.userProfile=userProfile;
}

从用户配置实体code:

Code from UserProfile Entity:

@OneToOne(mappedBy="userProfile",cascade=CascadeType.ALL)
public User getUser(){
	return this.user;
}

public void setUser(User user){
	this.user=user;
}

正如你所见,我对在用户配置的用户属性的cascadetype.all。但是,当我尝试删除用户配置实体,相应的用户实体仍然停留。 (当我尝试删除用户实体,相应的用户配置实体被删除。)

As you see, I have a cascadetype.all for the user attribute in UserProfile. But when I try deleting the UserProfile entity, the corresponding User entity still stays. (When I try deleting the User entity, corresponding UserProfile entity gets deleted.)

下面是我的问题: -

Here is my question:-


  • 请级联只能容纳当我指定他们?
  • 实体拥有的关系
  • Do cascades hold only when I specify them on the entity owning the relationship ?

推荐答案

您的问题是错误的本身,这是所有的混乱源于。亚瑟做了与他的回答一个很好的工作,但它是从注释中明确的混乱仍然如此让我来刺在这里。

Your question is wrong in and of itself, which is where all the confusion stems from. Arthur did a good job with his answer but it's clear from the comments the the confusion still remains so let me take a stab at it here.

做级联只能容纳当我指定
  他们对实体拥有该
  关系?

Do cascades hold only when I specify them on the entity owning the relationship?

层叠是你(在双向的情况下,或可能两者)的关系的端部上的一个指定的属性。它决定了操作在执行到底会传播到其他结束。有<一个href=\"http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-association-cascade\">many不同类型的这些行动在JPA和<定义href=\"http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-hibspec-cascade\">even Hibernate的扩展中定义多个。这种区别很重要 - 你应该只谈论的具体的行为被传播,而不是一般的层叠

"cascade" is an attribute you specify on one (or possibly both in case of bi-directional) end of a relationship. It determines what actions performed on that end will be propagated to the other end. There are many different types of those actions defined in JPA and even more defined in Hibernate extensions. This distinction is important - you should only talk about specific behavior being propagated and not "cascade" in general.

持续下去,MERGE,REFRESH正常传播(从他们宣布其他结束)。

PERSIST, MERGE, REFRESH propagate normally (from the end they were declared on to the other).

删除,但是,是棘手,因为它可能意味着两个不同的东西。如果你的 A 和你想删除的 A ,您可以删除的乙在另一端,也可以删除在关联,但留下的完好无损。
Hibernate会在两者之间有明显的区别 - 你可以单独声明删除(删除)和 DELETE_ORPHAN 级联类型; JPA规范没有。需要注意的是 DELETE_ORPHAN 不支持单值的关系(一对一/多对一)。

REMOVE, however, is tricky because it can mean two different things. If you have a relationship between A and B and you're trying to remove A, you can either remove B on the other end OR you can remove the association but leave B intact. Hibernate makes a clear distinction between the two - you can declare REMOVE (DELETE) and DELETE_ORPHAN cascade types separately; JPA spec does not. Note that DELETE_ORPHAN is not supported to single-valued relationships (OneToOne / ManyToOne).

因此​​,去除传播(本身或当它是所有的一部分)取决于关系是否有明确的所有者(单向总是这样,用它是否映射双向确实的mappedBy 并不,如果它通过在这种情况下,它从拥有者拥有或没有所有者在这种情况下,它在任何一个方向传播,但没有 DELETE_ORPHAN 连接表)映射C>语义,除非它被明确指定。后者的典型例子是双向的多对多

Thus, propagation of REMOVE (by itself or when it's part of ALL) depends on whether relationship has a clear owner (uni-directional always does; bi-directional does if it's mapped using mappedBy and does not if it's mapped via join table) in which case it's propagated from owner to owned OR no owner in which case it's propagated in either direction but without DELETE_ORPHAN semantics unless it was explicitly specified. Typical example of the latter is bi-directional many-to-many.

这篇关于在JPA级联的一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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