Difference between get and load method of Session interface in Hibernate OR Eager and Lazy Loading

Posted By : Parveen Kumar Yadav | 30-Jun-2015


There are two methods available of loading objects in Hibernate Session interface both are for loading object so the question came into mind is that what is the difference between them so in this blog i will discuss the difference between both:-


Both are using different approaches.


Hibernate support two strategies of loading objects:-

1)Eager loading

2)Lazy loading 


1.)In case of Eager loading state and relation of the requested entity are loaded from the DB, when the entity is requested from the session.

Eager loading is used when entities are to be detached i.e. when they are used in disconnected mode.

Detached entity:- if an entity(an object which contains data) is not known to the ORM Framework then it is said to be detached.

Get method provides the implementation of eager loading.


2)In lazy loading state and relation of the requested object are loaded when needed. when the entity is requested session creates a proxy of the entity and provides its reference to the user.


Lazy loading is used only in connected mode.In connected mode entities are not detached.

Load method provides the implementation of lazy loading.


So here are some differences between them-:

1)Get method provides the implementation of eager loading, whereas load() method provides the implementation of lazy loading.

2)Get method returned "null" if the entity of given id is not found in the Database, whereas load() method throws "object not found exception" on that case

3)In case of load method proxy of the requested object is returned by the session this object intercepts method calls i.e. if DM's are directly referenced from the proxy no data will be returned.

4)We cannot refer DM(Data member) directly when using Lazy method because of proxy so we always use getter method in case of lazy whereas in case of eager loading we can directly and via getter method access the DM.


For example:-

package mypack;
import util.*;
public class FetchDemo{
public static void main(String arr[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter Emp id:");
int id=in.nextInt();
Session session=MyFactory.getSession();
//Emp e=(Emp)session.get(Emp.class,id);
Emp e=(Emp)session.load(Emp.class,id);
System.out.println("State of entity");
System.out.println("Session is closed"); 


In this above example we have a class MyFactory which returns us the session object and we have also an EMP class. if we are fetching data via get() method than we can access directly also like e.name or e.job. but in case of load() we cannot access DM directly because there is a proxy object not actual so we use getter method as defined in above example.


About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..