开发者

many-to-many: 'parent' is deleted when there is still another 'child'

I have a many-to-many relationship where two records on one side have the same parent (the owner of the relationship) on the other side. When I delete one of the two records the parent and jointable records are also deleted. I don't want this. I expect that the parent will not be removed as long as there is a child on the other side.

I have the following data:

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

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

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

And this is the hibernate mapping

mapping of class A on table A:
<hibernate-map开发者_JAVA百科ping ...

        <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>

After I delete the object with SID '90' and NAME 'xyz' my data looks like this:

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
-----------------------   

I would expect that the record in table B will not be removed because it's not an orphan. What am I doing wrong?


use this in A Entity private List 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜