saveOrUpdate() method in Hibernate Session

saveOrUpdate

  • If the record is not present in the database, it will call save() method and inserts the record in the database.
  • If the record is present in the database, it will call update() method and updates the record in the database.

Example on saveOrUpdate:

Before executing the EmployeeSaveOrUpdateExample.java in dabase

Execute EmployeeSaveOrUpdateExample.java

After executing EmployeeSaveOrUpdateExample.java

Output in Eclipse

See last 2 lines , there is one insert statement which has done by save() method and one update statement which has done by update() method.

So saveOrUpdate() method calls save() method if there is no record in database, and it calls update() method if there is a record in database.

Posted in hibernate and tagged , , .

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">