Register now or log in to join your professional community.
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.
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.
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.
session.get() loads from database whereas session.load() won't
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.
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.
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.
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():
Session.get():