0
Sponsored Links


Ad by Google
This is again a very popular hibernate interview question, In my last post we have seen the another popular interview question, How many states of object in Hibernate.
Here we are going to explain you the differences between session.load() and session.get() method of Hibernate, which will helps you to select the best one of them.

Both method are used to fetch the records from the database and both are exist inside org.hibernate.Session interface,Session is a core interface in hibernate from here you can read more about all Core Classes and Interfaces of Hibernate 4
  1. session.load() method will return proxy object without hitting the database(proxy(fake) object with given unique identifier without initializing the properties of objects). Whereas session.get() method returns the actual row by hitting the database.
  2. After loading an object using session.load() method, if object is not found with given identifier and trying to access the properties of the object except that unique identifier than it will throws org.hibernate.ObjectNotFoundException Whereas session.get() method will return null value if object is not found with given identifier in database.

For example using session.load() method,

public static User findById(int id) {
  Session session = sessionFactory.openSession();
  User user = (User) session.load(User.class, id);
  
  System.out.println(user.getUserId());

  return user;
 }
From the above code sample, it will fulfill our point 1 difference, If you call findById(233) method by passing any identifier it will just create a fake object(proxy object) for you it will not verify with database that the actual Object is exist or not in database with 233 identifier.
And if you add one more line to access the another property of the object for example if we try to print the user name also by adding System.out.println(user.getUserName()); It will throws org.hibernate.ObjectNotFoundException which will complete our point 2 difference.

Actually load() method will not hit database unless and until we are not going to access the property of the object except identifier value. That is why printing the value of identifier will not throws any exception but printing the user name will throws an exception if object is not found in database.

For example using session.get() method,

public static User findById(int id) {
  Session session = sessionFactory.openSession();
  User user = (User) session.get(User.class, id);
  
  System.out.println(user.getUserId());

  return user;
 }

session.get() method will directly hit the database so calling findById(identifier) if object not found than it will return null.



Here is few popular Hibernate Interview Questions
What are the fetching strategies in Hibernate read more
Difference between save() and persist() method read more
What are the inheritance mapping supported by Hibernate read more
What are the caching providers supported by Hibernate read more

Sponsored Links

0 comments:

Post a Comment