Master java skills

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 tableMay Insert a new record or update an existing record
Returns the generated identifier as Serializable objectDoesn’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 objectReturn 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 contextIt is good in long-running conversations with an extended session context