• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

org.hibernate.StaleObjectStateException

 
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic