Hibernate get() vs. load(): Understanding the Differences, Study notes of Computer Science

The key differences between hibernate's get() and load() methods, focusing on their behavior, return values, and database interactions. Get() retrieves the original object from the database immediately, while load() returns a proxy object that is initialized from the database only when accessing its properties.

Typology: Study notes

2016/2017

Uploaded on 10/06/2017

suresh-kamble
suresh-kamble 🇮🇳

5

(2)

8 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Dierence Between Hibernate get
() and load() Methods ?
Hibernate »on Sep 18, 2014 { 91 Comments } By Sivateja
What is the dierence between hibernate get() and load() methods ? this is
one of the famous hibernate interview questions, most of us will use hibernate
get and load methods irrespective of knowing their exact behavior will you
accept that ? of course even myself too in the initial stages Today i will try to
clear that confusion.
Few Points About Hibernate get() & load()
Both are from Session interface, and we will call them as session.get() &
session.load()
Both will be use for retrieving the object (a row) from the database
Then what’s the dierence them ? lets start with load() and then get() method.
Consider a Student class having 3 properties stdId, stdName, stdCountry
1. session.load()
When you call session.load() method, it will always return a “proxy
object, whats the meaning of proxy object ?
Proxy means, hibernate will prepare some fake object with given identier
value in the memory without hitting the database, for example if we call
session.load(Student.class,new Integer(107)); > hibernate will create one
fake Student object [row] in the memory with id 107, but remaining
properties of Student class will not even be initialized, observe this
pf3

Partial preview of the text

Download Hibernate get() vs. load(): Understanding the Differences and more Study notes Computer Science in PDF only on Docsity!

Difference Between Hibernate get

() and load() Methods?

Hibernate »on Sep 18, 2014 { 91 Comments } By Sivateja

What is the difference between hibernate get() and load() methods? this is

one of the famous hibernate interview questions, most of us will use hibernate get and load methods irrespective of knowing their exact behavior will you accept that? of course even myself too in the initial stages Today i will try to clear that confusion.

Few Points About Hibernate get() & load()

  • Both are from Session interface, and we will call them as session.get() & session.load()
  • Both will be use for retrieving the object (a row) from the database

Then what’s the difference them? lets start with load() and then get() method. Consider a Student class having 3 properties stdId, stdName, stdCountry

1. session.load()

  • When you call session.load() method, it will always return a “proxy” object, whats the meaning of proxy object?
  • Proxy means, hibernate will prepare some fake object with given identifier value in the memory without hitting the database, for example if we call session.load(Student.class,new Integer(107)); > hibernate will create one fake Student object [row] in the memory with id 107, but remaining properties of Student class will not even be initialized, observe this

graphical representation…

  • It will hit the database only when we try to retrieve the other properties of Student object i mean stdName, stdCountry. If we call s2.getStdName() then hibernate will hit the database and search the row with student id 107 and retrieve the values, if object [row] not found in the database it will throws ObjectNotFoundException.,

Let me explain each point by taking an example

Example of session.load()

1 2 3 4 5

System.out.println("Student is calling with load()"); s2 =(Student) session.load(Student.class,new Integer(107)); System.out.println("Student called with load()"); System.out.println("Printing Student Name___"+s2.getStdtId()); System.out.println("Printing Student Name___"+s2.getStdName());

Output

Explanation:

In index.jsp > line number 4, i have called s2.getStdtId() and hibernate simply printed 107 [at Output > line number 3] without creating any database query why? because as i have explained hibernate will prepare some fake object with given identifier value in the memory without hitting the database. So when we