-->
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 Stale State Exception
PostPosted: Tue Mar 28, 2006 11:55 pm 
Newbie

Joined: Tue Mar 28, 2006 11:44 pm
Posts: 4
Hi all
I'm facing a problem while updating the object in hibernate
THe object1 has a mapping for another object2 as a set
I'm trying to to get the Object 1 and fetch the Object 2 from Object 1
updating the contents of Object2 , setting it back it Object1 and clling the update for Object1, I get an exception as "hibernate.stalestate Exception."
How can this problem be solved or if i'm doing anything wrong , how can that be corrected.
ANy help would be highly appreciated.

//gets the main object
oObject1= (Object1) Helpers.fetchObject1(ID);
//retrieves the object2 from object1
Object2 oObj2 = new Object2


Iterator it = oObject1.getObject2().iterator();
oData = (Object2) it.next();


String sEncGreeting = oData.getGreeting();

Set oSet = new HashSet();
oSet.add(oData);
oObject1.setObject2(oSet);

return oObject1;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:19 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The set returned from oObject1.getObject2() (which you throw away) is the persistent set associated with oObject1. The HashSet that you later create is transient, not associated with the DB at all. So when you save oObject1, hibernate is trying to update an in-memory persistent set from an in-memory transient set. Knowing that the user is always right, it assumes that it's in-memory persistent set reflects an old view of the database, and has to abort the save until you tell it what the actual DB state is.

Either use merge()/replicate() to save this, or don't call oObject1.setObject2(). Instead, you should use oObject1.getObject2().add(oData) to add an item to the set, or just edit oData without touching oObject1.getObject2() at all.

The general rule is: if an object is returned by hibernate, use that instance of the object for edits that you want to see down in the database. Creating new instances and getting them to overwrite in-memory instances is a tricky business, best avoided if possible.


Top
 Profile  
 
 Post subject: MySql stale state
PostPosted: Mon Apr 03, 2006 11:27 pm 
Newbie

Joined: Tue Mar 28, 2006 11:44 pm
Posts: 4
Regarding the same
What is to be done if the mysql connection goes stale afetr the wait_timeout of mysQl server is elapse?
How can i reconnect to the mysql server , when the server connection goes stale.
Currently i'm not using any connection pooling mechanism ,
so can this be done with the default mySql connectiions provided, or if i use c3p0 connection pooling.

Any help would be appreciated
Thanks in advance.
Jyoti


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 12:08 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you get a timeout exception, Hibernate insists that you rollback your transaction and close the session. At that point you can open a new session and proceed to use it. Reconnecting the same session isn't an option, you have to start a new session.


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.