Can anyone tell me what\'s the advantage of load()
vs get()
in Hibernate?
load will return a proxy object.
get will return a actual object, and returns null if it wont find any object.
The performance issues is also major difference between get and load method.
The get() method fetches data as soon as it’s executed while the load() method returns a proxy object and fetches only data when object properties is required. So that the load() method gets better performance because it support lazy loading. Whe should use the load() method only when we know data exists because it throws exception when data is not found. In case we want to make sure data exists we should use the get() method.
In short, you should understand the differential in between, and decide which method is best fix in your application.
I found this differences on the tutorial Difference between get and load method in Hibernate
Ex: if you are trying to load /get Empoyee object where empid=20. But assume record is not available in DB.
Employee employee1 = session.load(Employee.class,20); //Step-1
system.out.println(employee1.getEmployeeId(); //Step-2 --o/p=20
system.out.println(employee1.getEmployeeName(); //Step-3 -->O/P:ObjectNotFoundException
If you use load in step-1 hibernate wont fire any select query to fetch employee record from DB at this moment.At this pint hibernate gives a dummy object ( Proxy ). This dummy object doesnt contain anything. it is new Employee(20). you can verify this in step-2 it will print 20. but in step-3 we are trying to find employee information. so at this time hibernate fires a sql query to fetch Empoyee objct. If it is not found in DB.throws ObjectNotFoundException.
Employee employee2 = session.get(Employee.class,20); //Step-4
for session.get() hibernate fires a sql query to fetch the data from db. so in our case id=20 not exists in DB. so it will return null.
When Load is called it returns a Proxy object. Actual select query is still not fired. When we use any of the mapped property for the first time the actual query is fired. If row does not exist in DB it will throw exception. e.g.
Software sw = ( Software )session.load(Software.class, 12);
Here sw is of proxy type. And select query is not yet called. in Eclipse debugger you may see it like
sw Software$$EnhancerByCGLIB$$baf24ae0 (id=17)
CGLIB$BOUND true
CGLIB$CALLBACK_0 CGLIBLazyInitializer (id=23)
CGLIB$CALLBACK_1 null
CGLIB$CONSTRUCTED true
id null
prop1 null
softwareprop null
when I use
sw.getProp1()
the select query is fired. And now proxy now knows values for all the mapped properties.
Where as when get is called, select query is fired immediately. The returned object is not proxy but of actual class. e.g.
Software sw = ( Software )session.get(Software.class, 12);
Here sw is of type Software itself. If row exists then all mapped properties are populated with the values in DB. If row does not exist then sw will be null.
sw Software (id=17)
id Integer (id=20)
prop1 "prodjlt1" (id=23)
softwareprop "softwrjlt1" (id=27)
So as always said, use load only if you are sure that record does exist in DB. In that case it is harmless to work with the proxy and will be helpful delaying DB query till the mapped property is actually needed.
A: This is explained in the hibernate reference. One difference was performance and the other one is that load throws an unrecoverable Exception when no Object is found for the ID.
More details here
session.load(): It will always return a proxy object with the given identity value, even the identity value is not exists in database. However, when you try to initialize a proxy by retrieve it’s properties from database, it will hit the database with select statement. If no row is found, a ObjectNotFoundException will throw.
session.get(): It will always return null , if the identity value is not found in database.