• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

org.hibernate.StaleObjectStateException

 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I understand the exception itself, but not why it's being raised. Basically what I have is a couple of tables that I need to update from flat files so I build a list of objects from the flat file then loop through the that list to retrieve the database by an ID using a for-each loop. Since I am testing, the result set only has six cases in it and updating the first one seems okay, but when the loop iterates the second time and the new result set is attempting to be populated I get the exception. I would have expected it on a save or update, but not on retrieving the results of a named query. So, for instance...

Debugging shows the exception occurs at line three of the second iteration.
I hope this makes sense. TIA.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to do?
You already have the foo, so why are you getting it from the database again?

Debug that code by printing out the id of the foo each time round the loop (before doing the query).
Also show us the full stack trace, just in case there's something else in there.

I suspect it's the fact you are looping over a list of foos, and then selecting them each, and updating them.
That slightly odd route might be making one or the other foo object stale.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

I'm trying to get data from a fixed length flat file and update the database with that data. So...



The stack trace I'm logging is:


Right now I'm looking at the generated HQL to find test.stuff.Foo#0, and doing the debugging as you suggested.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Log the ids you are processing and see where exactly this is happening.
In other words, is it the first foo that exists in the db?
Is it a foo that you've already handled?
Is it the first foo after a flush?

Until you've debugged this you are simply guessing.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is with:


I mistakenly thought that by querying the database and getting a single item in the result set meant that I could update it with the stuff in foo (session.getNamedQuery, session.saveOrUpdate, the session know what I want by "magic" right?).

So instead, doing something like

And that takes care of the org.hibernate.StaleObjectStateException. Hope this helps others.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic