farzad wrote:
I am not sure about the removing and inserting part but cascade=none doesn't mean that. For that you need to set the inverse=true so that hibernate does not bother with this side of relation. For the strange behaviour, please explain more what happens in your code.
Farzad-
A little different sceanrio but it's kind of related.
I have a bidirectional mapping
Mapping file TCrmConfigEvents.hbm.xml has this association defined
<many-to-one
name="tcrmFixClientConfig"
class="com.TCrmFixClientConfig"
not-null="false"
>
Mapping file TCrmFixClientConfig.hbm.xml has this association defined
<!-- corresponding bidirection assosiation of above one-->
<set
name="auditTrail"
lazy="true"
inverse="true"
cascade="delete"
>
<key>
<column name="FixClientConfigId" />
</key>
<one-to-many
class="com.TCrmConfigEvent"
/>
</set>
<!--another mapping-->
<many-to-one
name="tcrmFixClient"
class="com.TCrmFixClient"
not-null="true"
>
<column name="CrmLinkId" />
</many-to-one>
Mapping file TCrmFixClient.hbm.xml has this association defined
<many-to-one
name="tcrmClient"
class="com.TCrmClient"
not-null="false"
>
Mapping file TCrmClient.hbm.xml has this association defined
<set
name="tcrmFixClients"
lazy="true"
inverse="true"
cascade="all"
fetch="subselect"
>
<key>
<column name="ClientId" />
</key>
<one-to-many
class="com.TCrmFixClient"
/>
</set>
Now I'm saving the TCrmConfigEvent as below..
TCrmConfigEvent event = new TCrmConfigEvent();
event.setUserId(userName);
event.setComments(comments);
event.setTimestamp(new Date());
event.setTcrmFixClientConfig(fcc); //fcc is an instance of TCrmFixClientConfig passed to this method
fcc.getAuditTrail().add(event);
session.save(event);
At this point hibernate generates the insert sql to insert a row in the corresponding table of TCrmConfigEvent, which is fine. But it also generates the 10 update sql to update TCrmFixClient object. TCrmClient has set of 10 TCrmFixClient objects. So hibernate seem to be reaching the object in the following path. TCrmConfigEvent-->TCrmFixClientConfig-->TCrmFixClient-->TCrmClient-->set of 10 TCrmFixClient.
My question is when I save TCrmConfigEvent why hibernate reach to TCrmFixClientConfig even though inverse=true on that association as show above? I'm trying to optimize the number of queries it generates. I just want to save the TCrmConfigEvent with a reference to TCrmFixClientConfig but don't want to save TCrmFixClientConfig or any associated object from there on.
I know it's bit detailed information but hopefully it convey the scenario as is.
Thanks