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?
Thank you,
John
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
just a guess: try:
protected Long id;
also, try <generator class="increment"/> or <generator class="native" /> [ June 10, 2005: Message edited by: miguel lisboa ]
java amateur
John Mikhail
Greenhorn
Joined: Jun 03, 2005
Posts: 20
posted
0
I tried that already. Tried all different types of generators, types, etc. Still nothing.
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
another guess: saveOrUpdate instead of just save() tried that too?
you mean after loading the object, making getId() returns null?
if i'm not wrong i remember that after transaction.commit() session gets syncronized with database... so id should be set... [ June 10, 2005: Message edited by: miguel lisboa ]
Originally posted by miguel lisboa: you mean after loading the object, making getId() returns null?
if i'm not wrong i remember that after transaction.commit() session gets syncronized with database... so id should be set...
[ June 10, 2005: Message edited by: miguel lisboa ]
Exactly. getID() returns null.
Mark
John Mikhail
Greenhorn
Joined: Jun 03, 2005
Posts: 20
posted
0
Here's the code snippet for what I'm trying to do:
John Mikhail
Greenhorn
Joined: Jun 03, 2005
Posts: 20
posted
0
Originally posted by miguel lisboa: you mean after loading the object, making getId() returns null?
if i'm not wrong i remember that after transaction.commit() session gets syncronized with database... so id should be set...
[ June 10, 2005: Message edited by: miguel lisboa ]
Well I'm not explicitly loading the object. I'm using the same instance I used to perform a save.
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
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 ]
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.
Mark
John Mikhail
Greenhorn
Joined: Jun 03, 2005
Posts: 20
posted
0
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 ]
Bridget Kennedy
Ranch Hand
Joined: Nov 30, 2004
Posts: 84
posted
0
I wonder what would happen if your column and name values were different.
I'm having the same problem with id not getting updated after a save. I define name and column, but the assigned values are not equal, for example: