May
24
2015

difference between session.get() and session.load() method in hibernate

In this post of hibernate we will see difference betwwen the two methods of fetching record from database. We will see difference between session.get() and session.load() method of fetching single record from DB. This is important question from interview prospective also.

He basic difference in these two approaches is that session.get() method fetch record from DB immediatly but session.load() method does not query to database immedialy instead it create a proxy (Dummy object) and return that dummy object. This dummy object does not contain value of actaul object. Here is image that will make clear

Difference between session.get and session.load method

Difference between session.get and session.load method

When we access data of object using getters then hibernate will query in database and then it will fetch actaul object from DB if it exist in database otherwise it will throw org.hibernate.ObjectNotFoundException: No row with the given identifier exists .

In case of session.getMethod if the record with given identifier does not exist then it does not throw ant exception instead it just return null.

Now let see it with example.

session.load()


 GetVsLoad.java

When we run above program and record does not exist with given id

I got proxy object.
Hibernate: select student0_.id as id0_0_, student0_.emailId as emailId0_0_, student0_.name as name0_0_, student0_.regNo as regNo0_0_ from Student student0_ where student0_.id=?
Exception in thread “main” org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.freakers.getvsload.Student#4]

As you can see from output when we use session.load() methos then hibernate query to database only when we access data of object not at the time the load() method is called. At time when load() method is called it just create proxy( Dummy object).

Let see output when the record with given primary key exist in DB.

I got proxy object.
Hibernate: select student0_.id as id0_0_, student0_.emailId as emailId0_0_, student0_.name as name0_0_, student0_.regNo as regNo0_0_ from Student student0_ where student0_.id=?
devendrapiet159@gmail.com

When we are accessing emailId from object then hibernate query DB and feetch actaul record from DB.

session.get()


In session.get() method hibernate query to dataBase immediatly as the get() method called. If record with given primary key does not exist the it will reurn null otherwise it will return actaul object. Let see same example with session.get() method

GetVsLoad.java

Here is output when the record with given primary ley does not exist in DB.

Hibernate: select student0_.id as id0_0_, student0_.emailId as emailId0_0_, student0_.name as name0_0_, student0_.regNo as regNo0_0_ from Student student0_ where student0_.id=?
I got original object.
Exception in thread “main” java.lang.NullPointerException

Here you can see hibernate query immediatly when get() method is called and it reurn null when record does not found in DB with given primary key.

Let see output when the record with given primary key exist in DB.

Hibernate: select student0_.id as id0_0_, student0_.emailId as emailId0_0_, student0_.name as name0_0_, student0_.regNo as regNo0_0_ from Student student0_ where student0_.id=?
I got original object.
devendrapiet159@gmail.com

Now question arise what is use of load method when we have get method to fetch record.

Let suppose there is a scenario where we are not accessing data of object we just need to assign the object to some other object like this

Here we are not accessing data of student object. Instead we are just assigning student object to course object. So in this case we done need to fetch this record from DB. Here load() method can be usefull because it is creating object (proxy object) without doing any query to DB.

Note: Make sure that this object really exist in DB in such scenario. Otherwise it will lead serious problem.

This is all in this post oh hibernate tutorial. Hope this is helpfull to you. I will get back with some more interesting and usefull hibernate tutorial.

Comments are closed.