-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How the cascade attribute shall be set?
PostPosted: Thu Dec 27, 2007 2:55 pm 
Newbie

Joined: Wed Feb 07, 2007 7:55 pm
Posts: 18
I have various one-to-many situations where parent/child update have different requirements and I currently only use the parent object merge for all changes on parent and child.

1) update on parent without any change on its childred. Shall I set cascade="delete-orphan"?
2) update on one child. Shall I set cascade="merge"?
3) add one child. cascade="save-update"?
4) delete one child. cascade="save-update"?

Shall I have a child DAO for 2 - 4 situation instead of relying on cascade setting?


Top
 Profile  
 
 Post subject: Re: How the cascade attribute shall be set?
PostPosted: Fri Dec 28, 2007 1:23 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
hm0 wrote:
I have various one-to-many situations where parent/child update have different requirements and I currently only use the parent object merge for all changes on parent and child.

1) update on parent without any change on its childred. Shall I set cascade="delete-orphan"?
2) update on one child. Shall I set cascade="merge"?
3) add one child. cascade="save-update"?
4) delete one child. cascade="save-update"?

Shall I have a child DAO for 2 - 4 situation instead of relying on cascade setting?



Cascade should be set in a way it defines reachability rules you want in your system. You can have a combination of cascade options or all of them. I have been in situations in which I prefer hibernate to persist everything in my object tree and in some situations I prefer to stop this cascading for performance reasons. What is your situation?



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 1:40 pm 
Newbie

Joined: Wed Feb 07, 2007 7:55 pm
Posts: 18
Thanks Farzard for your response.

That reason I would like to restudy the cascade setting is to improve performance. While the current related Hibernate configuration, including the cascade setting provide a right logic, I observe some unessential query statemenets generated by Hibernate. For example, the childrend are updated when only their parent data is updated. I try to have a right cascade setting to cut down those unessential query statements.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 4:46 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
hm0 wrote:
Thanks Farzard for your response.

That reason I would like to restudying the cascade setting is to improve performance. While the current related Hibernate configuration, including the cascade setting provide a right logic, I observe some unessential query statemenets generated by Hibernate. For example, the childrend are updated when only their parent data is updated. I try to have a right cascade setting to cut down those unessential query statements.


This can be more complicated than simply setting the right cascade option. Can you be more specific about the child update problem while updating a parent?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 5:01 pm 
Newbie

Joined: Wed Feb 07, 2007 7:55 pm
Posts: 18
I have a few of one-to-many relationship in one class. The mapping file is something like
Code:
<class name="Entry" table="entry">

...
   <set name="addresses" inverse="true" cascade="save-update, merge,delete-orphan">
      <key column="entry_id"/>
      <one-to-many class="AddressEntity"/>
   </set>
   
</class>

I only have a Hibernate DAO for Entry, but not for AddressEntity. When an Entry is updated, all its childred, addresses will be udpated with Hibernate generated SQL statements too although none of those address data is updated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 5:36 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
hm0 wrote:
I have a few of one-to-many relationship in one class. The mapping file is something like
Code:
<class name="Entry" table="entry">

...
   <set name="addresses" inverse="true" cascade="save-update, merge,delete-orphan">
      <key column="entry_id"/>
      <one-to-many class="AddressEntity"/>
   </set>
   
</class>

I only have a Hibernate DAO for Entry, but not for AddressEntity. When an Entry is updated, all its childred, addresses will be udpated with Hibernate generated SQL statements too although none of those address data is updated.


There is a few possibilities for this but my guess is that at the update point entry is a detached object therefore hibernate has no idea if its addresses have been modified. Hibernate can detect modifications if an entity exists in its first level cache. What I suggest is that you turn on caching for the collection and Entry entity and you set up hibernate to do a select before update. Merge might also have the same effect. With a second level cache the select before update has no cost but it will help hibernate find the modifications and prevent unnecessary updates. If this is not a solution for you then you will have to turn off cascades and try to persist entities manually. This will require you to decide what entities should be updated each time.

Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 31, 2007 3:12 pm 
Newbie

Joined: Wed Feb 07, 2007 7:55 pm
Posts: 18
Since I use Hibernate with Spring, a stateless web framework. Hibernate has no way to know any data is dirty or not. I will further investigate options you mentioned. According to my note, the second_level_cache shall set to false for some reason.


Thanks for your information/helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.