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

1 Answers
Answered by suresh

Difference between session.save() and session.persist() methods in Hibernate

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

In Hibernate, the session.save() and session.persist() methods are used to save an entity to the database. However, there are some differences between the two methods:

  • session.save(): This method returns the generated primary key value immediately after saving the entity to the database. If the entity object is already persistent, it will throw an exception.
  • session.persist(): This method does not return the primary key immediately; it is guaranteed to execute an INSERT statement, not to persist it immediately. If the entity object is already persistent, it will not throw an exception.

Therefore, the main difference between session.save() and session.persist() lies in their return values and behavior when dealing with persistent objects in Hibernate.

Answer for Question: What is the difference between session.save() and session.persist() methods in Hibernate?