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.
save and persist
- Difference – Return type
save | persist | |
---|---|---|
return type | Serializable | void |
- Same – both needs flush to be effective in database
save | persist | saveOrUpdate | |
---|---|---|---|
flush | Serializable 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(); |