-->
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.  [ 6 posts ] 
Author Message
 Post subject: Unwanted update to database
PostPosted: Mon Jan 16, 2012 1:59 pm 
Newbie

Joined: Mon Jan 16, 2012 1:41 pm
Posts: 6
Hello all,

I know this is a silly question and it must have already been discussed here somewhere. My apology but I don't have idea for good keywords to be used for googling. :P

My question is that I don't understand why my call to commit (without saving or updating prior to this commit call) updates my objects in the database. The scenario is that I loaded an object from a database, make some changes to it, and then change my mind not to save it. However, when the commit is called (I have this as a template), it persists the object onto my database.

I wonder if all changes are automatically detected when the commit is called, so what is the point of update or saveOrUpdate functions for the loaded objects.


Thanks


Top
 Profile  
 
 Post subject: Re: Unwanted update to database
PostPosted: Mon Jan 16, 2012 5:48 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Most of what you want to know is covered in 11.5 and 11.6 in the Hibernate documentation http://docs.jboss.org/hibernate/core/3. ... -modifying. If you change your mind and don't want to store changes to an object, you should call session.evict(object) before the session is committed/flushed.


Top
 Profile  
 
 Post subject: Re: Unwanted update to database
PostPosted: Wed Jan 18, 2012 4:27 am 
Newbie

Joined: Mon Jan 16, 2012 1:41 pm
Posts: 6
Hi nordborg,

I don't see any clear explanation there. I still feel it's a bit counter-intuitive. I'm expecting no update to database layer unless save alike functions are called. Otherwise, what is the objective of those functions for ?


Top
 Profile  
 
 Post subject: Re: Unwanted update to database
PostPosted: Wed Jan 18, 2012 6:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate implements automatic dirty checking so changes are automatically saved when the transaction is committed. This is a fundamental feature of Hibernate (and I suppose of any JPA-compliant implementation). Quote from the documentation:

Quote:
There is no need to call a particular method (like update(), which has a different purpose) to make your modifications persistent.



The save() and update() are used to associate transient or detached objects with a session.

Quote:
Many applications need to retrieve an object in one transaction, send it to the UI layer for manipulation, then save the changes in a new transaction
.....
Hibernate supports this model by providing for reattachment of detached instances using the Session.update() or Session.merge() methods


Top
 Profile  
 
 Post subject: Re: Unwanted update to database
PostPosted: Thu Jan 19, 2012 2:14 pm 
Newbie

Joined: Mon Jan 16, 2012 1:41 pm
Posts: 6
Thanks nordborg,

It's much clearer to me now.


Top
 Profile  
 
 Post subject: Re: Unwanted update to database
PostPosted: Thu Feb 02, 2012 4:21 am 
Newbie

Joined: Wed Mar 10, 2010 3:12 am
Posts: 3
Quote:
Many applications need to retrieve an object in one transaction, send it to the UI layer for manipulation


Which may break application layers... Yes it makes things fast and easy but regarding decoupling I would just warn you about sending entities to the UI layer... Which is another story of course :-)


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