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

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.

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.

Comments

comments

Comments

  1. Kamal Giri says:

    Good Explanation Keep Posting about Hibernate, Spring and JSF integration Example without build tool like Maven,ant. Thank you !

  2. If I think my Object is “dirty” and I want to update the Object from the database, is there a method that I can use?

  3. Alex Dodu says:

    Man, you saved my day! Thank you!

  4. Hi i have a table without primary key. i do insert data and cant able to update and delete.If you know guide me…

Speak Your Mind

*