Both session.get() and session.load() methods are used to fetch data from database, but there are some differences in their mechanism.
session.get()
- If no row found in database table then it returns 'null'.
- Loads data as soon as it is called.
- Always return 'real' object by hitting the database, so you will find select statement in the console/logs.
- We should use it when we want to make sure data exists in the database.
session.load()
- If no row found in database table then it throws 'ObjectNotFoundException'.
- Loads data only when it is actually required.
- Always returns 'proxy' object without hitting the database (properties of object are not initialized yet, a temporary object), so you will not find any select statement in the console/logs. If you try to retrieve its properties then only it will hit the database with select statement.
- We should use it only when we know data exists in the database.