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

后端 未结 7 2493
粉色の甜心
粉色の甜心 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:34

    You should apply the differnce between save() and saveOrUpdate method in your code to get the best performance:

    The save() method returns the identifier generated by the database. On the other hand, saveOrUpdate() can do INSERT or UPDATE depending upon whether object exists in database or not. And saveOrUpdate does a select first to determine if it needs to do an insert or an update. So you should use saveOrUpdate in case update query.

    Another key difference between save() and saveOrUpdate() method is that save() method is used to make a transient object to persistent state but saveOurUpdate() can make both transient (new object) and detached (existing object) object into persistent state. So that saveOrUpdate() is often used to re-attach a detached object into Session.

    From the post Difference between save and saveOrUpdate in Hibernate

提交回复
热议问题