aspose file tools*
The moose likes Object Relational Mapping and the fly likes Generated ID in JPA + Hibernate - updated ID in the original object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Generated ID in JPA + Hibernate - updated ID in the original object?" Watch "Generated ID in JPA + Hibernate - updated ID in the original object?" New topic
Author

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

Ondra Žižka
Greenhorn

Joined: Jul 17, 2008
Posts: 5
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

Joined: Jun 16, 2007
Posts: 106
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.


Thank you,
Reehan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generated ID in JPA + Hibernate - updated ID in the original object?