Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: cascade="none" deletes and reinserts the child obj
PostPosted: Tue Feb 19, 2008 4:44 pm 
Newbie

Joined: Thu Jan 31, 2008 1:48 pm
Posts: 11
I have cascade set to "none' for a set on a many-to-many relationship. When I save(session.update) the parent object I see hibernate issues a delete statement to delete all the child for the set and then issues insert statements to reinsert all the child objects. I thought cascade="none" should not touch the set objets for that associaion. I would appreciate any clue on this.

I'm using hibernate 3.2.6.ga, database sybase.

Thanks


Top
 Profile  
 
 Post subject: Re: cascade="none" deletes and reinserts the child
PostPosted: Tue Feb 19, 2008 6:35 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 734
Location: Montreal, QC
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-


Top
 Profile  
 
 Post subject: Re: cascade="none" deletes and reinserts the child
PostPosted: Wed Feb 20, 2008 1:37 pm 
Newbie

Joined: Thu Jan 31, 2008 1:48 pm
Posts: 11
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


Top
 Profile  
 
 Post subject: Re: cascade="none" deletes and reinserts the child
PostPosted: Wed Feb 20, 2008 2:27 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 734
Location: Montreal, QC
Again, it is not about inverse tag it is about cascade. So if you set the cascade option to none for:


Code:
<many-to-one
name="tcrmFixClientConfig"
class="com.TCrmFixClientConfig"
not-null="false"
cascade="none"
>


then you are good because hibernate will not try to reach any object from this association. However, I am wondering if you have other modifications in the session object and that's why other updates are being fired. In order to make sure you can call session.clear() before saving the event object and see if you observe the same behaviour. If nothing worked please send me the queries being fired and we will see what's going on there. In addition, please format your posts so that codes appear in a code box. It's really hard to read codes with normal fonts.



Farzad-


Top
 Profile  
 
 Post subject: Re: cascade="none" deletes and reinserts the child
PostPosted: Wed Feb 20, 2008 4:41 pm 
Newbie

Joined: Thu Jan 31, 2008 1:48 pm
Posts: 11
farzad wrote:
Again, it is not about inverse tag it is about cascade. So if you set the cascade option to none for:


Code:
<many-to-one
name="tcrmFixClientConfig"
class="com.TCrmFixClientConfig"
not-null="false"
cascade="none"
>


then you are good because hibernate will not try to reach any object from this association. However, I am wondering if you have other modifications in the session object and that's why other updates are being fired. In order to make sure you can call session.clear() before saving the event object and see if you observe the same behaviour. If nothing worked please send me the queries being fired and we will see what's going on there. In addition, please format your posts so that codes appear in a code box. It's really hard to read codes with normal fonts.

Farzad-


Farzad, sorry about the code format.
I tried cascade=none that did not work but session.clear() worked before saving the object. Thanks! But I don't understand what's going on here. I have not modified any object, I just save the screen as is. It creates the audit trail record (TCrmConfigEvent instance as shown above) on every save besides no other modification happen. Also i'm not sure session.clear is good idea in middle of your save operation (this scenario is okay but it might affect some other scenario ?)

Thanks for your reply, i got some clue!


Top
 Profile  
 
 Post subject: Re: cascade="none" deletes and reinserts the child
PostPosted: Tue Dec 09, 2008 6:46 pm 
Newbie

Joined: Wed Oct 08, 2008 3:06 pm
Posts: 13
Location: Seattle
srkamani wrote:
farzad wrote:
Again, it is not about inverse tag it is about cascade. So if you set the cascade option to none for:


Code:
<many-to-one
name="tcrmFixClientConfig"
class="com.TCrmFixClientConfig"
not-null="false"
cascade="none"
>


then you are good because hibernate will not try to reach any object from this association. However, I am wondering if you have other modifications in the session object and that's why other updates are being fired. In order to make sure you can call session.clear() before saving the event object and see if you observe the same behaviour. If nothing worked please send me the queries being fired and we will see what's going on there. In addition, please format your posts so that codes appear in a code box. It's really hard to read codes with normal fonts.

Farzad-


Farzad, sorry about the code format.
I tried cascade=none that did not work but session.clear() worked before saving the object. Thanks! But I don't understand what's going on here. I have not modified any object, I just save the screen as is. It creates the audit trail record (TCrmConfigEvent instance as shown above) on every save besides no other modification happen. Also i'm not sure session.clear is good idea in middle of your save operation (this scenario is okay but it might affect some other scenario ?)

Thanks for your reply, i got some clue!


Have you got a solution for that? I encountered delete-and-reinsert problem on @manytomany relationship as well..


Top
 Profile  
 
 Post subject: Re: cascade="none" deletes and reinserts the child obj
PostPosted: Mon Aug 20, 2012 5:18 pm 
Newbie

Joined: Tue Jun 26, 2012 7:52 pm
Posts: 3
I have the same issue... I tried all combination with cascade and inverse... hibernate is still deleting and reinserting al items related with the one I tried to select from the data base... I JUST WANT TO DO A SELECT!!!

_________________
---
Rodrigo


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.