Difference between get and load method of hibernate session

Posted By : Shiv Kumar | 13-Jun-2016

get() and load() both methods have same syntax and is used to load an entity using its id.But they are internally different in their mechanism.

public Object load(Class entityClass, Serializable id);
public Object get(Class entityClass, Serializable id);

 

Differences :

1. get() method provides the implementation of eager loading(load state and relations initially) whereas load() method provides the implementation of lazy loading(load state and relations when actaully needed).

2. When requested object is not found in the database get() method returns nullPointerException whereas load method throws objectNotFoundException.

3. get() method can be used in attached(session is active) as well as detached(session is closed) mode whereas load method can only be used in attached mode. In case of load() , if session is closed and then we try to access state of any object , LazyInitializationException is thrown.

 

I have following Emp table with 3 records.

Emp Table 
id  name     job         salary
1   Rahul    Developer   20000
2   Gaurav   Tester      19000
3   Gagan    Designer    20000

get() method Example :

public static void main(String arr[]){
Session session = MyFactory.getSession();
Emp emp = (Emp) session.get(Emp.class, 1);
//Emp emp = (Emp) session.get(Emp.class,5); It will throw nullPointerException beacuse id 5 is not available in db.
session.close();
System.out.println("Session closed, Entity state is :");
// State will be printed here becuase object is loaded eagerly with its own state and relations.
System.out.println(emp.getName()+" "+emp.getJob()+" "+emp.getSalary());
}

load() method Example :

public static void main(String arr[]){
Session session = MyFactory.getSession();
Emp emp = (Emp) session.load(Emp.class, 1);
//Emp emp = (Emp) session.load(Emp.class,5); It will throw objectNotFoundException beacuse id 5 is not available in db.
session.close();
System.out.println("Session closed, Entity state is :");
// LazyInitialization error will be thrown as object is loaded in lazy mode.
System.out.println(emp.getName()+" "+emp.getJob()+" "+emp.getSalary());
}

THANKS

About Author

Author Image
Shiv Kumar

Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.

Request for Proposal

Name is required

Comment is required

Sending message..