简体   繁体   English

如何删除两个实体之间的多对多关联?

[英]How to delete a many-to-many association between two entities?

Suppose I have two entities and a bi-directional many-to-many relation between them mapped in Hibernate. 假设我有两个实体,并且在Hibernate中映射它们之间的双向多对多关系。

The xml configuration looks like this in both classes: 两个类中的xml配置如下所示:

<hibernate-mapping>
    <class name="com.example.MyEntity">
        <set name="myOtherEntities" cascade="all-delete-orphan">
            <key column="entity_id"/>
            <many-to-many column="my_other_entity_id" class="com.example.OtherEntity" />
        </set>
    </class>
</hibernate-mapping>

How can I terminate only the association between them without deleting any of the entities? 如何在不删除任何实体的情况下终止它们之间的关联?

If I clear() the set in MyEntity and call Session.flush() then the MyOtherEntity objects get deleted but I only want to clear the records in the join table. 如果我clear() MyEntity的集合并调用Session.flush()那么MyOtherEntity对象将被删除,但我只想清除连接表中的记录。

I think it's the delete orphan property that trigger the deletion for MyOtherEntity . 我认为删除orphan属性会触发MyOtherEntity的删除。

Should be replaced by cascade="all" 应该用cascade="all"代替

Regards. 问候。

Addendum : Hibernate doc indicate in chapter 11 that cascade all-delete-orphan should not be used with ManytoMany relations. 附录:Hibernate doc在第11章中指出,cascade all-delete-orphan不应该与ManytoMany关系一起使用。

There are 2 ways using which you can do this. 有两种方法可以用来做到这一点。

1) Using Hibernate Native Query 2) Using Hibernate createSQLQuery() 1)使用Hibernate Native Query 2)使用Hibernate createSQLQuery()

The correct setting in this scenario is cascade="none" . 此方案中的正确设置是cascade="none"

Hibernate in many-to-many relations has to work with 3 tables. many-to-many关系中的Hibernate必须与3个表一起使用。 MyEntity , OtherEntity and pairing table MyEntity-OtherEntity . MyEntityOtherEntity和配对表MyEntity-OtherEntity

Pair table 配对表

Once we create the relation by adding MyEntity instance into the OtherEntity collection and vice versa, Hibernate will hiddenly insert the record into pairing table. 一旦我们通过将MyEntity实例添加到OtherEntity集合中来创建关系,反之亦然,Hibernate将隐藏地将记录插入到配对表中。 Once we remove some instance form the collection, Hibernate will remove the record from pairing table. 一旦我们从集合中删除了一些实例,Hibernate将从配对表中删除记录。

This is done, with the setting cascade="none" . 这样做,设置cascade="none" Becuase it is the only way how to handle that. 因为它是如何处理它的唯一方法。 There is no cascading from the perspective of the pairing table 从配对表的角度来看,没有级联

The cascading 级联

Any cascading applied on many-to-many relation, does not apply to pairing table, but to the second end. 任何应用于many-to-many关系的级联都不适用于配对表,而是适用于第二端。 In most cases, this is what we do not want. 在大多数情况下,这是我们不想要的。 We should have separated DAO for MyEntity management and OtherEntity management. 我们应该将DAO分离为MyEntity管理和OtherEntity管理。

In rare cases, when during update of one end, we want to insert, update or even delete the other... we can use cascading. 在极少数情况下,在更新一端时,我们要插入,更新甚至删除其他...我们可以使用级联。 But this is not our case. 但这不是我们的情况。

Use this setting in this scenario 在此方案中使用此设置

<set name="myOtherEntities" cascade="none">

See more 8.3.4. 查看更多8.3.4。 Many-to-many 许多一对多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM