Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generated ID in JPA + Hibernate - updated ID in the original object?

 
Ondra Žižka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I use Hibernate through JPA. The database is MySQL 5.0

I have an Entity with generated key. I create an object and perform merge(). JPA's way to give me the generated key is giving me the whole new object as a return value.

Is there a way to let Hibernate update my "old" object (i.e., the one passed to merge()) ? To have just

User = new User();
<jpa>.merge( user );

and now the user now has the ID set.

The reason I'm looking for this is that I would like to keep the old DAO API. The need to get the new object would cause much much code updates.

Thanks for tips,
Ondra
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got the set the key value for the newly created object. For example:

Then Hibernate will be able to identify that this object is already existing object and it just has to update the object. Else if there's no key or the key doesn't exist in DB, merge() will save it as a new object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic