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.  [ 2 posts ] 
Author Message
 Post subject: Semantics of Hibernate writes
PostPosted: Sat Mar 14, 2009 3:56 pm 
Newbie

Joined: Sat Mar 14, 2009 3:36 pm
Posts: 1
Hello hibernate community - I've been using hibernate for the last 12 months with a great deal of success. I just wanted to clarify some of the write semantics.

My understanding was that after hibernate pulls an object out of the database that object is not is not WRITTEN back to the database until I explicitly write it (in my case using getHibernateTemplate().update(myObject)). However, I noticed that whenever I update an object in memory it is written back to the database when my session is closed, even if I do not explicitly save it. I have confirmed this by looking at the SQL dumps.

Does this have to do with the FlushMode.MANUAL versus FlushMode.AUTO? In my case the flush mode is set to AUTO. If this is the case, why bother explicitly writing an object if flushing the session has the same effect?

I've searched for a solution / explanation in the docs and have extensively debugged my own system before asking this question. Many thanks for your responses.

regards,
anish

Hibernate version: 3

Name and version of the database you are using: mysql 5


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 12:17 pm 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

to understand that behaviour you have to take at look at the object-lifecycle in hibernate.
After creating an object via the new-operator hiberante doesn't know about this object and won't persist it - it's in "detached" state. Calling save() on that object just tells hibernate to recognize it as an object to be persisted (as update() would do in case of an already existing primary key), so hibernate will write the object and any changes to it to the database after it's own schedule - the object is now "attached" to the persistency-context.

When retrieving an object from the database using hibernate it is always in "attached" state (hibernate knows it's a persistent object), so all changes made to the object are written to the DB (when the session is flushed). If you don't want changes to be persisted you could detach() the object.

So: update()/save() are used only to attach previously detached objects to the persistency-context, so that all changes on that object will be persisted on the database.
Messing around with flush() or the FlushMode only makes sense if you want tight control over when hibernate actually persists the changes you made to your objects. For most cases you should just keep FlushMode at AUTO and hope that hibernate will take care of persisting your entities at the right time.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.