First I want to correct the mappings:
A.hbm.xml
<class name="A"
table="A">
<id
name="Id"
type="org.hibernate.type.IntegerType">
<column name="Id"/>
<generator class="increment"/>
</id>
<bag
cascade="none"
name="a"
table="a_b">
<key not-null="false" unique="false" update="true">
<column name="afk"/>
</key>
<many-to-many class="B" not-found="ignore">
<column name="bfk"/>
</many-to-many>
</bag>
</class>
B.hbm.xml
<class name="B"
table="B">
<id
name="Id"
type="org.hibernate.type.IntegerType">
<column name="Id"/>
<generator class="increment"/>
</id>
<bag
cascade="none"
name="a"
table="a_b">
<key not-null="false" unique="false" update="true">
<column name="bfk"/>
</key>
<many-to-many class="A" not-found="ignore">
<column name="afk"/>
</many-to-many>
Secondly,I managed to get rid of the problem by using
A.getB().removeAll();
sess.flush();
Now I have A without any b objects.
And in the same transaction I want to add some objects of B some of them might/might not be the same as the ones deleted.
So, before I do A.getB().add(b1), where b1 is an object of B type,
I have to load b1 by doing the sess.load(....).
As long as load the objects which were not there in A previously , it works fine, but as soon as I try to load one of the B object which was in A proviously, it throws an error , "a different object with the same identifier was associated in the session" .
Since I have already done sess.flush(). i thought it would have cleared the B objects in A. but thats not the case. Even sess.evict(A) does not seem to work for me.
|