What is the difference between session.save() and session.saveOrUpdate() methods in Hibernate?

Save method first stores an object in the database. Then it persists the given transient instance by assigning a generated identifier. Finally, it returns the id of the entity that is just created.

SaveOrUpdate() method calls either save() or update() method. It selects one of these methods based on the existence of identifier.

If an identifier exists for the entity then update() method is called. If there is no identifier for the entity then save() method is called as mentioned earlier.

Leave a Reply

Your email address will not be published. Required fields are marked *