• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Differences among save,update,saveorupdate,merge method in session object?

Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am new to hibernate and went thru hibernate tutorial in last week. I have few doubts about methods save,update,saveorupdate,merge in session object. These are:-

save method:- it is used to insert the newly created object in datastore.(Basically identifier value will be 0 for this). Like i create a new customer and call save operation , it will persist it in datastore and generate the identifier.Right ?.

But if we call save on already persistent object not sure what will happen?

update method:- it is used to update the already persitent object in datastore.(Basically identifier value will be some non zero value for this). Like i load a new customer and call update operation after update of some field value, it will update it in datastore .As per my understanding it should fail with some exception because as per api update is for detached object. Right?

if yes what should we call to update the object in the same session(i mean if object is not detached). Another point is not sure what will happen if we call update on newly created object?

saveorupdate method:- it will call either of above based on unsaved-value checks(which it must be doing based on identifier zero or non zero value. Right?) so if we have persistent customer object . Now if we update last name of his and create a new account also, then saveorupdate will take care of it. Correct?

Merge method :- it will like update but here if persistent object with the same identifier is already there in session it will update the detached object values in persistent object and save it. Right? But if If there is no persistent instance currently associated with the session, this will load the persistent object from datastore and then update the value of detached object in loaded persistent object and then update it. Correct?
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic