-->
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: Save and load an object multiple times in same transaction
PostPosted: Fri Nov 21, 2008 9:35 am 
Newbie

Joined: Fri Jun 16, 2006 9:59 am
Posts: 11
Hello,

What sould hibernate do in a situation like this to the result object?

Code:

//transaction, session, and identifier declared above
int i=0;
while (i < 10)
{

  Thing result = (Thing) session.load("Thing", identifier);

  //the following changes a field in the result object.
  result.incrementNumber();

  session.saveOrUpdate(result);

  i++;
}

//close session and transaction



Now, all of the loads and saves are in the same transaction without any flushes. After the first iteration of this loop, my number variable should be higher than it was before obviously. On the second attempt to do the load(), would I get the object with the number I just saved, or would I get the same object that I got during the first iteration of the loop?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2008 3:47 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
Thing result = (Thing) session.load("Thing", identifier);


This will always return the same instance.

Code:
session.saveOrUpdate(result);


This call is meaningless. It is only needed when you have a new unsaved object or an existing detached object. But .... doesn't Hibernate throw an exception here? The API docs says that it should if you call it with an object that is already associated with the session.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.