Save vs saveOrUpdate vs persist in Hibernate
Below is a comparision of save and saveOrUpdate methods.
Save | SaveOrUpdate |
---|---|
Save method generates a new identifier. | Not mandatory. May or may not generate an identifier. |
Inserts a new record in the table | May Insert a new record or update an existing record |
Returns the generated identifier as Serializable object | Doesn’t return anything. |
Save vs Persist
Save | Persist |
---|---|
Save method generates a new identifier immediately. | Persist method doesn’t guarantee that the identifier will be assigned immediately. The assignment of identifier can happen at flush time. However, persistence will surely happen. |
Returns the generated identifier as Serializable object | Return type is void |
Save might execute sql insert outside a transaction boundary also. | Persist will never execute an insert statement if it is called outside of a transaction boundary. |
It is not wise to use save method in long-running conversation with an extended session context | It is good in long-running conversations with an extended session context |