Saturday, 14 July 2018

What is the difference between get() and load() methods in Hibernate?

         In previous post, we learned the Hibernate Criteria API and it's example. In this, we can discuss difference between the get() and load() methods of Hibernate. This is one of the famous Hibernate interview question.

       In Hibernate Session, there are two methods for retrieving object from database i) get() and ii) load() method.  These two methods have been used in the different situations but both are from Session interface and we will call them as session.get() & session.load().

Session.load() method:


1) It will always return a proxy without hitting the database. In Hibernate, proxy is an object with   the given identifier value, its properties are not initialized yet, it just look like a temporary fake object.
2) load() method doesn’t hit the database.
3) If no row found , it will throws an ObjectNotFoundException.


Session.get() method:


1) It always hit the database and return the real object, an object that represent the database row, not proxy.
2) If no row found , it will returns null.
3) get() method always hit the database.
4) It returns real object not proxy.

get() method example:--


package com.adnblog;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.adnblog.HibernateUtil;

public class GetMethodHibernateExample {
    
     public static void main(String args[]) {
        
        //Create SessionFacctory
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        
        //Create Session from SessionFactory
        Session session = sessionFactory.openSession();
        
        //Begin the transaction
        session.beginTransaction();
        
        //Create Employee object
        Employee employee = (Employee) session.get(Employee.class,new Integer(4322));
        System.out.println("Employee Details:--");
        System.out.println("Employee Id   : "+employee.getId());
        System.out.println("Employee Name : "+employee.getName());
        System.out.println("Employee Age  : "+employee.getAge());
        System.out.println("Department    : "+employee.getDept());
        
        //Commit the changes
        session.getTransaction().commit();
        //Close the session
        session.close();
    }
}


Output : -

Hibernate: select employee0_.id as id1_0_0_, employee0_.AGE as AGE2_0_0_, 
employee0_.DEPT as DEPT3_0_0_, employee0_.NAME as NAME4_0_0_ from EMPLOYEE employee0_ 
where employee0_.id=?
Employee Details
Employee Id   : 4322
Employee Name : JIP
Employee Age  : 100
Department    : IT

       In the above code, we have used session.get() to retrieve the employee with the ID 4322, Hibernate immediately hits the database and returns the original Employee object. This is the reason, we are getting the below SQL query immediately when we retrieve the employee id (employee.getId()).

Hibernate: select employee0_.id as id1_0_0_, employee0_.AGE as AGE2_0_0_, 
employee0_.DEPT as DEPT3_0_0_, employee0_.NAME as NAME4_0_0_ from EMPLOYEE employee0_ 
where employee0_.id=?


load() method example:-


package com.adnblog;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.adnblog.HibernateUtil;

public class loadMethodHibernateExample {
    
     public static void main(String args[]) {
        
        //Create SessionFacctory
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        
        //Create Session from SessionFactory
        Session session = sessionFactory.openSession();
        
        //Begin the transaction
        session.beginTransaction();
        
        //Create Employee object
        Employee employee = (Employee) session.load(Employee.class,new Integer(4322));
        System.out.println("Employee Details:--");
        System.out.println("Employee Id   : "+employee.getId());
        System.out.println("Employee Name : "+employee.getName());
        System.out.println("Employee Age  : "+employee.getAge());
        System.out.println("Department    : "+employee.getDept());
        
        //Commit the changes
        session.getTransaction().commit();
        //Close the session
        session.close();
    }
}

Output :-
Employee Details:--
Employee Id   : 4322
Hibernate: select employee0_.id as id1_0_0_, employee0_.AGE as AGE2_0_0_, 
employee0_.DEPT as DEPT3_0_0_, employee0_.NAME as NAME4_0_0_ from EMPLOYEE employee0_
 where employee0_.id=?
Employee Name : JIP
Employee Age  : 100
Department    : IT

         In the above code, we have used session.load() to retrieve the employee with the ID 4322, Hibernate immediately created the fake Employee object with the Id 4322 and the remaining properties would not have been initialized.
         It will hit the database only when it tries to retrieve other properties of Employee object.



Related Posts:--
1)  Hibernate Criteria Queries and Examples
2) Hibernate Query Language(HQL) Examples
3) What is a Hibernate Caching ? Explain first level and second level cache in Hibernate
4) What are different states of an entity bean in Hibernate?
5) Hibernate One to One Mapping Example - Annotation based
6) Hibernate - JPA Annotations with explanation
7) Advantages of Hibernate over JDBC

No comments:

Post a Comment