What are the differences between get and load methods in hibernate? This is a common interview question also.
I will share few points here and please feel free to add more points if you have.
Answer
Both get and load methods can be used to select data from a database through hibernate. Invocation is also very similar. Please refer to http://javajee.com/crud-operations-in-hibernate-43 for an example for get.
Below are few of the differences:
Get immediately hit the database, fill your object with the data and return that object to you. It can be considered as an eager fetch. So you can close the session after the invocation and still use the retrieved object to access the elements. Load does not hit the database immediately and return you only a proxy object. A proxy object is another object that can be assigned to your reference will have some additional functionality added. In case of load method, the database hit will be made only once you try to access the members of the object. Therefore if you close the session after the invocation and try to use the retrieved object object, you will get an exception that the session is not available.
Second difference is that get will return a null object in case data is not available in database for that table, load will throw an exception is data is not available in database for that table.
Please add more more points if you have. Try this out and comment with the exact exception you will get if you try the negative cases mentioned here.