如何在休眠中的多对一映射中定义逆级联删除 [英] how to define an inverse cascade delete on a many-to-one mapping in hibernate

查看:83
本文介绍了如何在休眠中的多对一映射中定义逆级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类A和B.许多B可以与单个A有关联,因此是从B到A的多对一关系。我已经将关系映射为:

 < class name =Atable =tbl_A> 
< property name =propAcolumn =colA/>
< / class>
< class name =Btable =tbl_B>
< property name =propBcolumn =colB/>
< / class>

A没有任何映射B.保持这一点,我们打算删除B时,它是关联的是删除。如果我可以在B中的多对一关联中定义一个inverse =true,但是hibernate不允许的话,这可能是可能的。

任何人都可以帮助有了这个?我们不希望为此写入任何内容。

解决方案

Hibernate只能沿着定义的关联进行级联。如果A对Bs一无所知,那么你对A所做的任何事情都不会影响Bs。



因此,帕斯卡的建议是最简单的方法来做你想做的事情:

 < class name =Atable =tbl_A> 
...
< set name =myBsinverse =truecascade =all,delete-orphan>
< key column =col1/>
<一对多课程=B/>
< / set>
< / class>

< class name =Btable =tbl_B>
...
< / class>

请注意,设置 cascade =delete B 上,因为你在原始代码中拥有它将不会做你想做的事情 - 它告诉Hibernate删除A,如果B被删除,这很可能导致违反约束(如果有任何其他Bs与该A链接)。



如果绝对不能将A的集合添加到A(尽管我无法真正想到这种情况在那种情况下),唯一的另一种选择是在外键级别定义级联从A删除到B;当你的A被删除时,你的Bs将被删除。



然而,这是一个相当难看的解决方案,因为你必须非常小心你如何在Hibernate中删除A :


  1. 在删除A之前,会话必须刷新(具有挂起的更新到B可能会导致错误或A,插入幕后)

  2. 必须将所有与您的A链接的B(并且因为您没有保持与A方的关系,这意味着所有 B)必须被驱逐来自所有活动会话和二级缓存。


I have two classes A and B. Many B's can have association with a single A, hence a many-to-one relationship from B to A. I've mapped the relationship like:

<class name="A" table="tbl_A">
  <property name="propA" column="colA"/>
</class>
<class name="B" table="tbl_B">
  <property name="propB" column="colB"/>
  <many-to-one name="a" class="A" column="col1" cascade="delete"/>
</class>

A has nothing mapped to B. Keeping this in mind we intend to delete B when it's associated A is deleted. This could have been possible if I could define an inverse="true" on the many-to-one association in B but hibernate does not allow that.

Can anyone help with this? We do not want to write anything in A for this.

解决方案

Hibernate only cascades along the defined associations. If A knows nothing about Bs, nothing you do with A will affect Bs.

Pascal's suggestion is, therefore, the easiest way to do what you want:

<class name="A" table="tbl_A">
  ...
  <set name="myBs" inverse="true" cascade="all,delete-orphan">
    <key column="col1"/>
    <one-to-many class="B"/>
  </set>
</class>

<class name="B" table="tbl_B">
  ...
  <many-to-one name="a" class="A" column="col1" not-null="true"/>
</class>

Note that setting cascade="delete" on B as you have it in your original code will NOT do what you want - it tells Hibernate to "delete A if B is deleted" which is likely to result in constraint violation (if there are any other Bs linked to that A).

If you absolutely cannot add a collection of Bs to A (though I can't really think of the circumstances where that'd be the case), your only other alternative is to define cascade delete from A to B at the foreign key level; your Bs will then be deleted when your A is deleted.

This is a rather ugly solution, however, because you have to be extremely careful of how you delete A in Hibernate:

  1. Session must be flushed prior to deleting A (having pending updates to B may result in an error or A and some Bs being re-inserted behind the scenes)
  2. All Bs linked to your A (and since you're not maintaining the relationship from A side that means all Bs) must be evicted from all active sessions and 2nd level cache.

这篇关于如何在休眠中的多对一映射中定义逆级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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