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 id is not set after getSession().save() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "id is not set after getSession().save()" Watch "id is not set after getSession().save()" New topic
Author

id is not set after getSession().save()

zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
I was wondering if anyone could help me with a hibernate problem:

The problem is that the id of the object is not set after save. For instance:





Here is how the id is defined in AreaType.hbm.xml






I also tried using using my own SequenceGenerator but it didn't help.





I am using Hibernate 3, Postgresql 8.3

I also tried flushing after every save, but that has no effect.


Any ideas?

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Welcome Greenhorn!

What happens in the database? Is the record getting saved after the transaction has been committed with a unique id, or is everything failing?

Any log errors?

-Cameron McKenzie
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
Hi,

The object is saved successfully in the database (with an id).

there is no exception when I save the object to database. it's just that the id is null but it shouldn't be.
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
Hibernate executes the following queries:

14:27:28,813 INFO [STDOUT] Hibernate: select nextval ('public.area_type_seq')
14:27:28,829 INFO [STDOUT] Hibernate: insert into public.area_type (name, id) values (?, ?)

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Well, I wonder...

Tell me where I'm wrong in this line of reasoning:

Calling save doesn't actually save right away. Instead, the object is just put into the Hibernate Session and scheduled for a save. So, you could still change properties of that object, and Hibernate won't save until you're done. Or, you could just delete the object before the transaction is committed, and Hibernate can just never add it at all, avoiding a needless database interaction. So, the id doesn't actually get set in the object until the transaction is committed. I'm not sure if the API promises to initialize the id before the commit.

Here's what the JavaDoc for the Hibernate Session says:


Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.)


JavaDoc for the Hibernate Session

Maybe the sequence generator falls into the domain of "or using the current value of the identifier property."

The save method is supposed to return the id that's going to be used. What happens if you do this:



I'm not convinced that will work though. I think null becomes the identifier property when you use a sequence right up until the transaction is committed.

Let us know what happens. It's an interesting situation.

-Cameron McKenzie
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
Thanks for your reply

When I do



the id is set with the correct value, so the Id is there but not set automatically!


I have never had this problem with hibernate before, I don't know where my mistake could be.

Could the dialect play a role in this? I am using instead of
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

You know, maybe this is the very reason why the save method returns the id that is used, rather than just depending upon the developer to call the getter for the id in the POJO being persisted.

-Cameron McKenzie
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
So if I understand you correctly, you are saying that it is ok for hibernate to expect me to set the Id?

Ok but I have the same problem with getSession().saveOrUpdate(). and this method doesn't return an id, so how do i do this manually?


There must be a way to get hibernate to set the id.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

iririr zz please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
just an update on this,

apparently, this has nothing to do with my hbm file, flushing/saving or postgresql. I created a test project with spring and hibernate, and tested on the same hbm file object and table/sequence. the id gets set after



So only one route left: It must have something to do with Jboss and ejbs. I don't know exactly what is not permitting the id's to get set, but the version field (which is used by hibernate) does get set on object with a version.


I am still investigating
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
Well finally, I figured out what is happening,

I perform the save from inside an ejb, and the id is set as soon as the save call is made.

however as soon as I leave the ejb (back to the client ), the id disappears!!

Here is the scenario:

unit test calls ejb to save an object.
id is set inside ejb.

unit test checks if the id is null or not.
id is null.

...i guess it has to do with how RMI works, but don't have a clue how to get the id to persist in the unit test.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: id is not set after getSession().save()
 
Similar Threads
Hibernate is saving the id as -E.000E-78
Hibernate: Updates all the items of a set - Cascade ‘create’ not working
Hibernate: using oracle sequence
How to represent a sequence in oracle in HQL
Hibernate Mapping for id