Differences among save,update,saveorupdate,merge method in session object?
posted 5 years ago
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?
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads