Differences among save, update, saveOrUpdate, merge methods in Session?

后端 未结 7 2407
粉色の甜心
粉色の甜心 2020-12-04 08:50

I am new to Hibernate and went through the Hibernate tutorial last week. I have a few doubts about methods save, update, saveOrUpdate and merge in the Session class. These a

相关标签:
7条回答
  • 2020-12-04 09:45

    session.update() - It is used for a scenario when you have load an object Person1 from hibernate session. Now it is being used in application - may be on client side also, has been updated. We want to save it again. We know that no change has been done in Person object in data base. So we can simply use update.

    session.merge() - In above scenario, if changes have been done in person data before saving the changed Person1 object, then we should use merge. It will merge the changes.

    session.save() - It can be used to save a new object. It returns the serialiable identity.

    session.persist() - It is same as save(), but is void method and does not return anything.

    session.saveOrUpdate() - This method will work for both new and old objects. If object is new, it will work like simple save or if object is already persistent, it will work like update.

    session.lock() - It is used only to take the lock on object or you can say to check the version of object. It is not meant for updating the object. It should be used to reattach the object, only if you are sure that object state is not changed in database already. Otherwise, it may override the changes. < Inviting more points on this.

    0 讨论(0)
提交回复
热议问题