Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: ID not set upon save

 
John Mikhail
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a guess: try:

protected Long id;

also, try <generator class="increment"/> or
<generator class="native" />
[ June 10, 2005: Message edited by: miguel lisboa ]
 
John Mikhail
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that already. Tried all different types of generators, types, etc. Still nothing.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another guess: saveOrUpdate instead of just save()
tried that too?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, we have a correction. It is not the ID field in the database that isn't getting set, it is the object not getting this ID set in its attribute.



Mark
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code snippet for what I'm trying to do:

 
John Mikhail
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

<class name="CommMessage" table="comm_message">
<id
column="comm_msg_id"
name="id"
type="integer"
>
<generator class="native" />
</id>
 
Jack Wiesenthaler
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed you're using generator of type sequence but not specifying the sequence name to use. How is this working?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic