Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Tuesday, July 8, 2014

Difference between session.update() , session.merge() and session.saveOrUpdate() method of hibernate API.


What is the difference between session.update(Object obj), session.merge(Object obj) and session.saveOrUpdate(Object obj) method of hibernate API?

session.update(), session.merge() and session.saveOrUpdate() all three methods are used to update the records in database using hibernate persistence logic.


List of differences :

session.update(Object obj) method will update the record if the record is found in database, if record is not found in database it will throw an unchecked exception.it will not return anything as part of method call because its return type is void.

session.merge(Object obj) method will update the record if the record is found in database, if record is not found in database it will add that particular record in table, also it returns a persistent object of java pojo class whose object is going to insert in table. 

session.saveOrUpdate() method will update the record if the record is found in database, if record is not found in database it will add that particular record in table, but does not returns any thing as result of method call because its return type is void.

Prototype of session.update(Object object) method

public void update(Object object)throws HibernateException

Prototype of session.saveOrUpdate(Object object) method


public void saveOrUpdate(Object object)throws HibernateException

Prototype of session.merge(Object object) method


public Object merge(Object object)throws HibernateException

Example using session.update(Object obj) method:

import org.hibernate.*;
import org.hibernate.cfg.*;         
public class TestClient 
{
          public static void main(String[] args) 
          {
                   Configuration cfg= new Configuration();
                   cfg=cfg.configure("Hibernate.cfg.xml");
                   SessionFactory factory=cfg.buildSessionFactory();
                   Session ses=factory.openSession();
                   Transaction tx=ses.beginTransaction();
                   EmpBean eb2=new EmpBean();
                   eb2.setNo(125);
                   eb2.setFname("die");
                   eb2.setLname("hard");
                   eb2.setMail("diehardtechy@gmail.com"); 
                   ses.update(eb2); 

/* If record 125 is found in table then it will update the record , otherwise it will throw an exception. */

                   tx.commit();
                   ses.close();
                   factory.close();

          }
}

If record not found in database


Example using session.saveOrUpdate(Object obj) method:


import org.hibernate.*;
import org.hibernate.cfg.*;         
public class TestClient 
{
          public static void main(String[] args) 
          {
                   Configuration cfg= new Configuration();
                   cfg=cfg.configure("Hibernate.cfg.xml");
                   SessionFactory factory=cfg.buildSessionFactory();
                   Session ses=factory.openSession();
                   Transaction tx=ses.beginTransaction();
                   EmpBean eb2=new EmpBean();
                   eb2.setNo(125);
                   eb2.setFname("die");
                   eb2.setLname("hard");
                   eb2.setMail("diehardtechy@gmail.com"); 
                   ses.saveOrUpdate(eb2); 

/* If record 125 is found in table then it will update the record , otherwise it will add the record in the table. */
                   tx.commit();
                   ses.close();
                   factory.close();

          }
}

Example using session.merge(Object obj) method:

import org.hibernate.*;
import org.hibernate.cfg.*;         
public class TestClient 
{
          public static void main(String[] args) 
          {
                   Configuration cfg= new Configuration();
                   cfg=cfg.configure("Hibernate.cfg.xml");
                   SessionFactory factory=cfg.buildSessionFactory();
                   Session ses=factory.openSession();
                   Transaction tx=ses.beginTransaction();
                   EmpBean eb2=new EmpBean();
                   eb2.setNo(125);
                   eb2.setFname("die");
                   eb2.setLname("hard");
                   eb2.setMail("diehardtechy@gmail.com"); 
                   EmpBean ee=(EmpBean)ses.merge(eb2); 

/* If record 125 is found then it will update the record in db, otherwise it will add that record in database, it doesn't throw any exception if the record in not found. Also it returns the persistence pojo class object whose record is going to be inserted in DB.*/

                   System.out.println(ee);
                   tx.commit();
                   ses.close();
                   factory.close();

          }
}

All the above example java program are based upon the assumption that you have EmpBean.java as pojo class. If you don't have the EmpBean.java class or you are new to hibernate you can first read our article on How to start with Hibernate.


No comments: