• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Difference between hibernate’s save,update and saveOrUpdate() methods

September 27, 2008 //  by JavaBeat//  Leave a Comment

Difference between hibernate’s save,update and saveOrUpdate() methods

Hibetnate has set of methods for saving and updating the values in the database. The methods look like same and difficult to differentiate between them if you are not understanding them clearly. If you understand the hibernate mechanism clearly, it doesn’t execute the SQL statements directly to manipulate the database, hibernate itself a state maintaining mechanism. Hibernate just holds the state of the new object, loaded object and updated object. The methods which you are using are for updating its states and ultimately updated in database after the unit of work is completed. That happens when flush method is called. If you want to receive the future articles on hibernate, please subscribe here.

  • Hibernate Interview Questions
  • Hibernate Framework Books
  • Introduction to Hibernate

save

Save method stores an object into the database. That means it insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update

Update method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn’t exist, it will throw exception.

saveOrUpdate

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. saveOrUpdate() method does the following:

  • If the object is already persistent in the current session, it do nothing
  • If another object associated with the session has the same identifier, throw an exception to the caller
  • If the object has no identifier property, save() the object
  • If the object’s identifier has the value assigned to a newly instantiated object, save() the object
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * source : www.javabeat.net
 */
public class HibernateExample {
    public static void main(String args[]){
	Configuration configuration = new Configuration();
	SessionFactory  sessionFactory =
           configuration.configure().buildSessionFactory();
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	transaction.begin();
	EmployeeInfo employeeInfo = new EmployeeInfo();
	employeeInfo.setSno(1);
	employeeInfo.setName("HibernateTestSave");
	session.save(employeeInfo);
	transaction.commit();
	session.close();

	session = sessionFactory.openSession();
	transaction = session.beginTransaction();
	transaction.begin();
	employeeInfo = new EmployeeInfo();
	employeeInfo.setSno(1);
	employeeInfo.setName("HibernateTestUpdate");
	session.update(employeeInfo);
	transaction.commit();
	session.close();

	session = sessionFactory.openSession();

	transaction = session.beginTransaction();
	transaction.begin();
	employeeInfo = new EmployeeInfo();
	employeeInfo.setSno(1);
	employeeInfo.setName("HibernateTestSaveOrUpdate");
	session.saveOrUpdate(employeeInfo);
	transaction.commit();

	session.close();
   }
}

If you want to receive the future articles on hibernate, please subscribe here.

  • Hibernate Framework Books

Category: HibernateTag: Hibernate

Previous Post: « How to enable SSL on JBoss application server?
Next Post: Many-to-One Relationship in Hibernate Mappings – Example »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact