开发者

Why is hibernate deleting rows from join table when adding element to set mapping many-to-many?

开发者 https://www.devze.com 2023-01-22 14:25 出处:网络
S开发者_JAVA技巧uposse I have two classes: class A { Set<B> bs } class B { } This mapping: <set name=\"bs\" table=\"bs_tab\" cascade = \"save-update\">

S开发者_JAVA技巧uposse I have two classes:

class A {
Set<B> bs
}
class B {
}

This mapping:

<set name="bs" table="bs_tab" cascade = "save-update">
            <key column="a_id />
            <many-to-many column="b_id" class="B"/>
</set>

And join table like this:

bs_tab(
a_id, b_id, primary key(a_id, b_id)
)

When I add some element to bs set and then call Session.saveOrUpdate(A instance) hibernate is deleting all rows in bs_tab coresponding to B instances that were in the set before adding new element.

How can I solve this?


Make sure to implement equals/hashCode correctly. I have the same kind of mapping (unidirectional many-to-many) and adding elements does not generate DELETE then INSERT SQL statements for the join table.

0

精彩评论

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