What is the difference between session.save() and session.saveOrUpdate() methods in Hibernate?
Answered
What is the difference between session.save() and session.saveOrUpdate() methods in Hibernate?
Best answer
In Hibernate, session.save() method can be used to save a new object in the database. It will persist the given transient instance, first assigning a generated identifier. It returns the id of the entity created.
The session.saveOrUpdate() method will work for both new and old objects. This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called. If object is new, it will work like simple save method or if object is already persistent, it will work like update method.