@ManyToOne JPA协会和级联...如果我删除对象不知道会发生什么 [英] @ManyToOne JPA association and cascade... not sure what happens if I delete an object

查看:1324
本文介绍了@ManyToOne JPA协会和级联...如果我删除对象不知道会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然不是级联删除操作的工作方式非常清晰。我在想,如果我有这个会发生什么:

 类myBean {    @ManyToOne(取= FetchType.LAZY,的cascade = CascadeType.ALL)
        公共Cliente getClienteDiAppartenenza(){
            返回clienteDiAppartenenza;
        }}
类Cliente {
@OneToMany(的cascade = CascadeType.ALL,orphanRemoval = TRUE,取= FetchType.LAZY)
    公开名单< myBean> getMyBeans(){
        返回myBeans;
    }
}

如果我删除与此属性myBean我不知道如果链接Cliente将(在多对一怪异)太删除或内部Cliente集合将被更新,并myBean的该实例中删除,然后将其保存。

会发生什么事? Hibernato文档是不是这个很清楚......


解决方案

这不是Hibernate的东西,这是JPA 2.0标准的一部分。你在你的注释两个方面,一方面是利用orphanRemoval的。

您使用orphanRemoval当父实体拥有子实体的创建和销毁的控制。在UML这将是组合物的情况下是由整个部分的一个强有力的所有权和concident寿命。在第2.9 JPA 2.0规范:实体的关系说:


  

这是被指定为协会
  一对一或一对多支持使用
  在orphanRemoval选项。该
  应用以下行为时
  orphanRemoval实际上是:


  
  

      
  1. 如果是这种关系的目标是从删除的实体
      关系(通过设置
      关系为空或删除
      从关系的实体
      集合),删除操作
      被施加到实体感
      而成为孤儿。删除操作
      在冲洗时施加
      操作。该orphanRemoval
      功能是为实体
      这是私人拥有其
      父实体。便携式应用
      否则必须不依赖于
      去除特定的顺序,而且必须
      不重新分配一直是一个实体
      孤儿到另一个关系或者
      否则试图坚持它。如果
      成为孤儿的实体是
      独立,新的或删除实体时,
      orphanRemoval语义不
      适用。


  2.   
  3. 如果删除操作应用到管理的源实体,删除
      操作将被级联到
      按照目标的关系
      3.2.3节的规则,(因而
      这是没有必要指定
      级联=删除了
      关系)[20]。


  4.   

第二个方面是使用cascase的=当没有orphanRemoval隐含REMOVE。

本节3.2.3:删除包含有关删除过程的详细信息:


  

移除操作的语义,
  应用到实体X与如​​下:


  
  

•如果X是一个新的实体,它被忽略
  通过删除操作。然而,
  删除操作级联到
  用X引用的实体,如果
  从X到这些关系
  实体被标注与
  级联=删除或级联= ALL
  注释元素的值。


  
  

•如果X是一个管理实体,删除
  操作使其成为去除。
  删除操作级联到
  用X引用的实体,如果
  从X到这些其他关系
  实体被标注与
  级联=删除或级联= ALL
  注释元素的值。


  
  

•如果X是一个离散的实体中,
  抛出:IllegalArgumentException会
  通过删除操作抛出(或
  事务提交会失败)。


  
  

•如果X是已删除的实体,它
  通过移除操作忽略。


  
  

一个删除实体X将被删除
  从在或之前的数据库
  事务提交或作为的结果
  刷新操作。一个实体后,
  已被去除,它的状态(除了
  生成的状态)将是的
  在该点的实体在该
  删除操作被调用。


I'm still not very clear about the way cascade works in deletion operations. I was wondering what happens if I have this:

class myBean{

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        public Cliente getClienteDiAppartenenza() {
            return clienteDiAppartenenza;
        }

}


class Cliente{
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    public List<myBean> getMyBeans() {
        return myBeans;
    }
}

if I delete the myBean with this property I'm not sure if the linked Cliente will be deleted too (weird in a manytoone) or the collection inside Cliente will be updated and that instance of myBean removed, then saved.

What will happen?? Hibernato docs aren't very clear about this...

解决方案

This is not a Hibernate thing, this is part of the JPA 2.0 standard. You have two aspects in your annotations, one thing is the use of orphanRemoval.

You use orphanRemoval when the parent entity has control over the creation and destruction of the child entity. In UML this would be a case of composition that is a strong ownership and concident lifetime of the parts by the whole. The JPA 2.0 specification in section 2.9: Entity Relationships says:

Associations that are specified as OneToOne or OneToMany support use of the orphanRemoval option. The following behaviors apply when orphanRemoval is in effect:

  1. If an entity that is the target of the relationship is removed from the relationship (by setting the relationship to null or removing the entity from the relationship collection), the remove operation will be applied to the entity being orphaned. The remove operation is applied at the time of the flush operation. The orphanRemoval functionality is intended for entities that are privately "owned" by their parent entity. Portable applications must otherwise not depend upon a specific order of removal, and must not reassign an entity that has been orphaned to another relationship or otherwise attempt to persist it. If the entity being orphaned is a detached, new, or removed entity, the semantics of orphanRemoval do not apply.

  2. If the remove operation is applied to a managed source entity, the remove operation will be cascaded to the relationship target in accordance with the rules of section 3.2.3, (and hence it is not necessary to specify cascade=REMOVE for the relationship)[20].

A second aspect would be the use of cascase=REMOVE when no orphanRemoval is implied.

The section 3.2.3: Removal contains details about the remove process:

The semantics of the remove operation, applied to an entity X are as follows:

• If X is a new entity, it is ignored by the remove operation. However, the remove operation is cascaded to entities referenced by X, if the relationship from X to these other entities is annotated with the cascade=REMOVE or cascade=ALL annotation element value.

• If X is a managed entity, the remove operation causes it to become removed. The remove operation is cascaded to entities referenced by X, if the relationships from X to these other entities is annotated with the cascade=REMOVE or cascade=ALL annotation element value.

• If X is a detached entity, an IllegalArgumentException will be thrown by the remove operation (or the transaction commit will fail).

• If X is a removed entity, it is ignored by the remove operation.

A removed entity X will be removed from the database at or before transaction commit or as a result of the flush operation. After an entity has been removed, its state (except for generated state) will be that of the entity at the point at which the remove operation was called.

这篇关于@ManyToOne JPA协会和级联...如果我删除对象不知道会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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