Difference between load() and get() in hibernate is one of the popular interview questions for both who attends telephonic as well as face to face interview. Here are list of differences if you find any more differences more than this you can post in comments section. I will include those valid comments in this tutorial.
|Transaction tx = session.beginTransaction();Employee employee = (Employee) session.load(Employee.class, new Long(294912));
Employee employee1 = (Employee) session.get(Employee.class, new Long(294912));
|If object is present in the database it return object otherwise it returns null.||If object is present in the database it return object otherwise it returns objectNotFoundException if object is not found.load() never returns null.|
|get() never returns proxy object. Proxy object means object which has id or primarykey property and remaining properties are uninitialized.||load() always returns proxy object.|
|get() eagerly loads the data i.e always hit the database||load() lazily loads data.load hits the database on demand i.e whenever we are calling getter methods on pojo other than id property,proxied object gets initialized.|
|Performance wise it is not recommended to use get because it always hits the database.||Performance wise it is recommended to use load because load will hit the database on demand.|
|We have four overloaded methods for get() method.||We have five overloaded methods .public void load(Object object, Serializable id) throws HibernateException
This is an extra method which takes object as argument.
When to use get() and load() in database?
Both get() and load() are used to retrieve records from database.
1) Use get() only when we are sure about record is existed in database other wise it is better to use load() method.
2) load() performs lazy loading performance it is recommended to use load() method rather than get() which hits database everytime record is requested.