Thursday, August 20, 2009

Difference between hibernate's save,update and saveOrUpdate() methods

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 method will be called.

Note:

  if you use "unsaved-value="null" " property in .hbm xml this
SaveOrUpdate will not work


EX: <id name="accountID" column="ACCOUNT_ID" type="java.lang.String"
unsaved-value="null">

 

No comments: