In this post, we will see differences between get and load in hibernate. It is most asked interview question on hibernate.
Tutorial Content:
- Introduction to hibernate framework
- Hibernate hello world example in eclipse
- Difference between openSession and getCurrentSession
- Hibernate one to one mapping example
- Hibernate one to many mapping example
- Hibernate many to many mapping example
- Hibernate inheritance:Table per class hierarchy
- Hibernate inheritance:table per subclass
- Hibernate inheritance:Table per concrete class
- Difference between openSession and getCurrentSession
- Difference between get and load
- Spring MVC Hibernate MySQL CRUD example
- Spring Rest hibernate example
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
|
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.