Home > AI > Backend > SpringBoot > mysql-connector-java >

Difference between save, persist, and saveOrUpdate

save and saveOrUpdate

The main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record. Clearly, saveOrUpdate is more flexible in terms of use but it involves extra processing to find out whether a record already exists in the table or not.

Read more: https://javarevisited.blogspot.com/2012/09/difference-hibernate-save-vs-persist-and-saveOrUpdate.html#ixzz7LzbUYn2O

save and persist

  • Difference – Return type
savepersist
return typeSerializable void
  • Same – both needs flush to be effective in database
savepersistsaveOrUpdate
flushSerializable s = session.save(person);
session.flush();
or
transaction.commit();
session.persist(person);
session.flush();
or
transaction.commit();
session.saveOrUpdate(person);
session.flush();
or
transaction.commit();

Leave a Reply