• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic