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

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

Ninja Asked on 18th September 2018 in Hibernate.
Add Comment
1 Answer(s)
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.

Ninja Answered on 18th September 2018.
Add Comment