Difference between get and load in hibernate

In this post, we will see differences between get and load in hibernate. It is most asked interview question on hibernate.

Tutorial Content:

Before we actually see differences, let me give you brief introduction of both.

session.get()

  • session.get() method always hits database and returns actual object
  • It returns null in case it does not get object.

session.load()

  • session.load() method always does not hit database and returns proxy object
  • It throws  ObjectNotFoundException  in case it does not get object.

 get vs load in hibernate

Parameter
get
load
Database retrieval
It always hits the database
It does not hit database
If null
If it does not get the object with id, it returns null
If it does get the object with id, it throws ObjectNotFoundException
Proxy
It returns real object
It returns proxy object
Use
If you are not sure if object with id exists or not, you can use get
If you are sure about existence of object, you can use load

Add Comment