When you call the session.load() method, it always returns a proxy object. What does a proxy object mean? Proxy means that hibernate will prepare some fake object with the given identifier value in memory without affecting the database, for example, if we call.
session.load(Student.class,new Integer(107));
hibernate will create in memory one fake Student [row] object with identifier 107, but the rest of the Student class properties will not even be initialized, pay attention to this graphical representation ...
It will get into the database only when we try to get other properties of the Student object, I mean stdName , stdCountry .
If we call s2.getStdName() then hibernate will go to the database, s2.getStdName() search for the line with student ID 107 and get values; if object [row] not found in the database, it will ObjectNotFoundException .
session.get()
When you call session.get() , it immediately enters the database and returns the original object. If the row is not available in the database, it returns null .
So which method is best to use, hibernate load() or get() ? It is completely your choice.
source share