• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving hibernate generated id

 
anuja Edathu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using generator class, assigned in my mapping file . how can i retrieve the generated id after persisting the object to database . Do i need to take it from database again ?
Thanks & Regards
Anuja K
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the object should have it in it already after Hibernate writes it. Although not sure if it is right away or after it has been committed.

Mark
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be available just after Session.save(object) is called.


Cheers
-Praful
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session.save() will return the ID it assigned (just in case you do not have accessor methods in your persistent entity). but as the others mentioned: it's already assigned after the call to Session.save().

another method that could be of interest to you if your entitiy is associated with a session: Session.getIdentifier()
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html


pascal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic