File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate: ID not set upon save Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: ID not set upon save" Watch "Hibernate: ID not set upon save" New topic
Author

Hibernate: ID not set upon save

John Mikhail
Greenhorn

Joined: Jun 03, 2005
Posts: 20
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

Joined: Feb 08, 2004
Posts: 1281
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
I tried that already. Tried all different types of generators, types, etc. Still nothing.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
another guess: saveOrUpdate instead of just save()
tried that too?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
Here's the code snippet for what I'm trying to do:

John Mikhail
Greenhorn

Joined: Jun 03, 2005
Posts: 20
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
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
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
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

Joined: Jul 26, 2001
Posts: 75
I noticed you're using generator of type sequence but not specifying the sequence name to use. How is this working?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: ID not set upon save