Thursday, October 23, 2014

Hibernate LOAD VS GET methods


Hibernate LOAD VS GET Methods

Most of the time the developers mix use the session.get() and session.load() hibernate methods, while they develop the applications. If they know the difference between the both of the methods, its may help to them to use those method properly. Not only that but also,  most of the technical interviews the developers faced the question regarding the difference between session.get() and session.load() methods..

In this tutorial the main objective is that, developers need to understand the session.get() and session.load() methods before they going to use in future.

But before going to list down the properties, both session.get() and session.load() methods are going to use for retrieve the objects. But there are bit differences.

The below table list down the session.get() method properties.

Get
get() involves database hits if the object does not in Session Cache, and return fully initialized object.
get() method returns null if object is not found in cache as well as on database
get() method is not return the proxy, but its returns fully initialized object or null.
Use of get() is little less performance compare with load()
Execution :
1
2
Session session = SessionFactory.getCurrentSession();
Student student = (Student) session.get(Student.class, StudentID);

The below table list down the session.load() method properties.

Load
load() can return proxy in place and only initialize the object or hit the database if any method other than getId() is called on persistent or entity object.
load() method throws ObjectNotFoundException instead of null, if object is not found on cache as well as on database
load() method may return proxy, which is the object with ID but without initializing other properties
Use of load() is littel better performance compare with get()
Execution :
1
2
Session session = SessionFactory.getCurrentSession();
Student student = (Student) session.load(Student.class, StudentID);

No comments:

Post a Comment