I'm trying a very simple insert of an object and I'm noticing that the ID of the object is not being set in my pojo after the call to save (actually a call to Session.save or Session.persist, although Session.save does return the serializable ID). I thought that hibernate was supposed to set the ID after the object is persisted? Am I wrong in my understanding?
Here's what my object looks like:
And my mapping...
Yet when I run my test and make a call to persist or save, the object is saved correctly in the database, but the id field is not set. Can someone please shed some light on this?
Originally posted by miguel lisboa: one more guess: try closing/reopening session before assert - not sure if that would forced you to (re)load obj...
because it seems to me you are asserting the oobj that's in memory, not the one just persisted, and so former has not yet an ID...
[ June 10, 2005: Message edited by: miguel lisboa ]
Yes but you pass the instance to the save, so wouldn't that also update the instance to include the ID, and/or, the commit() doesn't that update all the DTOs to be refreshed with the saved data? Because if you didn't have the ID, then you wouldn't be able to get it because you would have needed it to lookup the value in the database. Catch-22.
Joined: Jun 03, 2005
I figured out the problem. Here's the solution in case anyone else runs into this problem.
Turns out my mapping file was somewhat incorrect. Actually it wasn't incorrect, it was missing something. Before it looked like:
Turns out instead of specifying column="id", I should have said name="id". I assumed that if have column in there, that Hibernate would know what attribute to set in the pojo. I figured since it works that way if I specified only name, that it would work the other way around. So now my mapping looks like:
So if you don't want to waste a whole day tracking down this problem, remember to include name in there [ June 10, 2005: Message edited by: John Mikhail ]