Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Can I return the generated key with Hibernate?

 
Roberto Hernandez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm inserting records in the database but I need to know how I can return the generated key using Hibernate. I'm currently using prepared statement to return the key as a string:

After the query is executed, I use this code to return back the generated key for the record I just inserted


I need to be able to do the same thing with Hibernate but I don't know how. Can anyone please help???

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate does this for you, you don;t need to do anything.
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roberto Hernandez,

You can use merge method of Session interface,



In the above code ,
nsb is the object to persist.
nsbC is the the object to return a copy of persisted object.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
show me your hibernate code...then may be i can help you, furthermore check this
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anbarasu aladiyan wrote:Hi Roberto Hernandez,

You can use merge method of Session interface,



In the above code ,
nsb is the object to persist.
nsbC is the the object to return a copy of persisted object.


Why do you need to do this? The save method will return the value of the generated identifier. Calling merge is an unecessary step.
 
Roberto Hernandez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
This is the code that I'm trying to convert to Hibernate. I'm fairly new to Hibernate so please bear with me.

the dao.insertApplication() returns the generated key as a String (appKey) because it will be a foreign key in the next insert of this transaction (dao.insertAppModuleInfo), and the generated key (moduleKey) will also serve as a foreign key for dao.insertModuleFunctionality() and dao.ModuleBusArea(). Am I making myself clear? There's probably a better way to do this in hibernate, I just don't know how. I'm hoping you all can help.
Thanks,
 
Charan kumar sarvepalli
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

if you are gerenating your AppId using key generators then it automatically hibernate does for you.

for e.g.
i have a POJO



I am not setting user_id becoz it is auto generated by database sequence or hibernate incerement

just after calling the session.save(user)

if i want user_id i just now user user.getUserId() it will return user id generated by hibernate while inserting.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic