gpt4 book ai didi

java - 多对多 : 'parent' is deleted when there is still another 'child'

转载 作者:行者123 更新时间:2023-12-01 05:41:47 25 4
gpt4 key购买 nike

我有一个多对多关系,其中一侧的两条记录在另一侧具有相同的父级(关系的所有者)。当我删除两条记录之一时,父记录和可连接记录也会被删除。我不想要这个。我希望只要另一边有 child , parent 就不会被删除。

我有以下数据:

Table A                                      
SID | NAME
-----------------------
90 | xyz
-----------------------
91 | abc

Table AB
A_SID | B_SID
-----------------------
90 | 5
-----------------------
91 | 5

Table B
SID | NAME
-----------------------
5 | lala

这是 hibernate 映射

mapping of class A on table A:
<hibernate-mapping ...

<set name="setOfBs" table="AB" inverse="true" cascade="delete-orphan">
<key column="A_SID" not-null="true" />
<many-to-many column="B_SID" class="B"/>
</set>
...
</hibernate-mapping>

mapping of class B on table B:
<hibernate-mapping ...

<set name="setOfAs" table="AB" lazy="false">
<key column="B_SID" not-null="true" />
<many-to-many column="A_SID" class="A"/>
</set>
...
</hibernate-mapping>

删除 SID '90' 和 NAME 'xyz' 的对象后,我的数据如下所示:

05/08/2011 07:29:43,259 [DEBUG] SQL - delete from AB where B_SID=?
05/08/2011 07:29:43,259 [TRACE] BasicBinder - binding parameter [1] as [BIGINT] - 5
05/08/2011 07:29:43,264 [DEBUG] SQL - delete from B where SID=? and OBJECT_VERSIE=?
05/08/2011 07:29:43,265 [TRACE] BasicBinder - binding parameter [1] as [BIGINT] - 5
05/08/2011 07:29:43,265 [TRACE] BasicBinder - binding parameter [2] as [BIGINT] - 0
05/08/2011 07:29:43,275 [DEBUG] SQL - delete from A where SID=? and OBJECT_VERSIE=?
05/08/2011 07:29:43,275 [TRACE] BasicBinder - binding parameter [1] as [BIGINT] - 90
05/08/2011 07:29:43,275 [TRACE] BasicBinder - binding parameter [2] as [BIGINT] - 0



Table A
SID | NAME
-----------------------
91 | abc

Table AB
A_SID | B_SID
-----------------------

Table B
SID | NAME
-----------------------

我希望表 B 中的记录不会被删除,因为它不是孤儿记录。我做错了什么?

最佳答案

在实体中使用它 私有(private)列表 Bs = new ArrayList(0);

@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinTable(name = "AB", joinColumns = { @JoinColumn(name = "A", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "B", nullable = false, updatable = false) })
public List<B> getBs() {
return this.Bs;
}

public void setBs(List<B> Bs) {
this.Bs = Bs;
}

关于java - 多对多 : 'parent' is deleted when there is still another 'child' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6952387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com