-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate: Cascade save-update question
PostPosted: Thu Feb 17, 2005 1:49 pm 
Newbie

Joined: Wed Sep 29, 2004 10:10 am
Posts: 13
Location: Washington D.C. USA
I have a question about Hibernate saving/updating entire object tree:

1. The presentation layer requests a deep copy of parent object, then it modifies the parent object as well as some of its children objects as per the form data. The session at this point is closed.
2. After modifying the parent/child objects, the presentation layer calls businees and then Data layer to save it.
3. The data layer will again gets the session associated with the same thread. As the session is already closed, it will open a new session and does the saveOrUpdate operation.

In above scenario, I am worndering if Hibernate will update entire tree of parent object or will it only update the corresponding tables of modified objects ?

I do not see dirty bits in the POJO itself, so wondering how Hibernate will ever know the object's set method is called out of the session. If it does not, will it update unnecessary tables which might be an overhead.

Thank you.
Ram Mahajan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 5:31 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
When the session closes, POJOs that were fetched from the database move into a detached state. At that point, if the objects or deserialized copies of them are "reattached" to a new session via a saveOrUpdate(), Hibernate will attempt to save all of the children/descendants according to cascade rules that you've set up in the mapping files. The Hibernate in Action book covers this topic in detail.

This is the price paid for transparent persistence using POJOs. Theoretically, it may be inefficient, but in practice, I don't think it's common for large object graphs to be passed back and forth between the presentation and business layers in a web app.


Top
 Profile  
 
 Post subject: Solution to cascade save-update and overhead
PostPosted: Fri Feb 18, 2005 1:02 pm 
Newbie

Joined: Wed Sep 29, 2004 10:10 am
Posts: 13
Location: Washington D.C. USA
I think I have a simple solution so that setting cascade=save-update option does not create an overhead of saving/updating unnecessary tables.

I am using Middlegen create POJOs. If this POJOs have a variable defined say 'modified_flag' as one of its attribute which gets set to true if any of the object's set method is called. It can be done by simply adding 'modified_flag = true' line in each set method of the object.

Hibernate then regardless whether in the same session or in the new session will check the flag and take necessary action avoiding unnecessary updates to the tables.

Any flaws in this approach, guys ?

-- Ram Mahajan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 1:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you want to do this, use Interceptor.isDirty()


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