Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between get and load method in Hibernate?

user-image
Question added by Deleted user
Date Posted: 2014/08/29
Muhammad Waqas
by Muhammad Waqas , C#/.Net Developer , Signup solution

Session.load()

1. It will always return a “proxy” (Hibernate term) 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.If no row found , it will throws an ObjectNotFoundException.

 

Session.get()

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 return null.

Ibraheem Umar
by Ibraheem Umar , Java developer , Micros

  • When you call session.load() method, it will always return a “proxy” object.Proxy means, hibernate will prepare some fake object with given identifier value in the memory without hitting the database.It will hit the database only when we try to retrieve the other properties of the object.
  • When you call session.get() method, it will hit the database immediately and returns the original object.
  • If the row is not available in the database, it returns null.

Manish Chandra
by Manish Chandra , Principal Architect , Wipro Ltd.

The differences between get() and load() methods are given below.

No.get()load()1) Returns null if object is not found. Throws ObjectNotFoundException if object is not found. 2) get() method always hit the database. load() method doesn't hit the database. 3) It returns real object not proxy. It returns proxy object. 4) It should be used if you are not sure about the existence of instance. It should be used if you are sure that instance exists.

aldrin apsay
by aldrin apsay , IT officer , eyecrafter optical inc.

 

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.

Kranthi Kumar Chettukindi
by Kranthi Kumar Chettukindi , Programmer analyst , cognizant technology solutions

session.get() loads from database whereas session.load() won't

durga yamini komera
by durga yamini komera , Associate software engineer , vijay infoart solutions

It will always return a “proxy” (Hibernate term) without hitting the database. InHibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. If no row found , it will throws an ObjectNotFoundException.

Salman Abdul Waheed
by Salman Abdul Waheed , Technical Manager , sensys

Load

It return the proxy object without hitting data base

Get

It hit the database and return objects or null, object represent row of database null if no row found 

 

get() is the eager initializer method.Get() is the special method which is used for retrieving the data from the database, get going to take two parameters as input i.e. class name and a primary key id.

Shamrooque R P
by Shamrooque R P , Apps Systems Engineer 6 , Wells Fargo

1. Session.load does not hit the database it returns the proxy of pojo object representing database row using the given identifier value whereas session.get always hit the database and it returns actual database object representing the row

2. session.load catches ObjectNotFoundException when no row is found whereas session.get returns null when no row is found.  

 

NOOR AHAMED S N
by NOOR AHAMED S N , Software Developer , S A Tech Software (I) Pvt Ltd

Get() is eagerly initializer, Get() is used for retrieval purpose.Get is the special method which is used to retrive the data from the data base,get going to take two parameters as inputs,i.e class name and a primary key id

Load() is Lazy initializer .

In hibernate session there are two methods for retrieving object from database one is get() and other 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():

  • 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.

Session.get():

  • It always hit the database and return the real object, an object that represent the database row, not proxy.

More Questions Like This