Difference between get and load in hibernate

Last updated on March 24th, 2017 at 10:38 pm

Previous
Next

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

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog