Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iririr zz please check your private messages.
 
zabet tyan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic