24 Apr 2015

Difference between get() and load() methods of Session in Hibernate

Both session.get() and session.load() methods are used to fetch data from database, but there are some differences in their mechanism.

session.get()

  1. If no row found in database table then it returns 'null'.
  2. Loads data as soon as it is called.
  3. Always return 'real' object by hitting the database, so you will find select statement in the console/logs.
  4. We should use it when we want to make sure data exists in the database.

session.load()

  1. If no row found in database table then it throws 'ObjectNotFoundException'.
  2. Loads data only when it is actually required.
  3. 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.
  4. We should use it only when we know data exists in the database.





Popular Posts

Write to Us
Name
Email
Message