Friday, 27 March 2015

Save, Update, SaveOrUpdate, persist, merge

save:
Save method stores an object into the database. That means it insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update:
Update method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn’t exist, it will throw exception.

saveOrUpdate:
This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save 

persist:
Make a transient instance persistent

merge:
suppose we create a session and load an object. Now object in session cache. If we close the session at this point and we edit state of object and tried to save using update() it will throw exception. To make object persistent we need to open another session. Now we load same object again in current session. So if we want to update present object with previous object changes we have to use merge() method. Merge method will merge changes of both states of object and will save in database. 

No comments:

Post a Comment