get() & load() method in hibernate

in ORM like Hibernate , we can easily retrieve any existing record using get() or load() with using PRYMARY KEY present in database.

But when to use get() and when to use load() ?

what is the main difference between get() and load()?

to find the answer of those questions i have performed some coding practical to understand the basic differences.

suppose the given code example represent the get() and the load() method

SessionFactory factory=HibernateUtil.getSessionFactory();

System.out.println(factory);

Session session=factory.openSession();

Transaction tx= session.beginTransaction();

//get student by id using get()

// System.out.println("++++++get student data using id using get()++++++");

//Student st= session.get(Student.class, 1);

//Student st1 = session.get(Student.class,1);

// System.out.println(st);

//System.out.println(st1);

//get student by id using load()

System.out.println("++++++get student data using id using load()++++++");

Student st= session.load(Student.class, 1)

; Student st1= session.load(Student.class, 1);

// System.out.println(st);

//System.out.println(st1);

in case of get() method:

each time the object with a particular session_id is not present or not available in the database then it will return NULL as result.

in case of load() method:

each time the object with a particular session_id is not present or not available in the database then it will return ObjectNotFoundException() as result.

when to use get() method:

when you are not sure about the query that you have entered with an id, is present in the database or not then it is better to use get() method to fetch data from database.

when to use load() method:

when you are sure about the query that you have entered with an id, is present in the database then it is better to use load() method to fetch data from database.

case 1:

//get student by id using get()

// System.out.println("++++++get student data using id using get()++++++");

//Student st= session.get(Student.class, 1);

//Student st1 = session.get(Student.class,1);

// System.out.println(st);

//System.out.println(st1);

in this case -

value will be stored in session.

sql query will only execute once.

get() method will perform egar loading that is why the query will execute even if you do not want to print the object values.

case 2:

//get student by id using load()

System.out.println("++++++get student data using id using load()++++++");

Student st= session.load(Student.class, 1)

; Student st1= session.load(Student.class, 1);

// System.out.println(st);

//System.out.println(st1);

in this case:

value will be stored in session.

sql query will run only once.

load() method will perform lazy loading that is why it will not execute the sql query if you do not call the values to print .

so those are some of the difference that I have found , hope it will help readers to understand and use Hibernate wisely in there project work.

if you found something helpful in this post then pls. like ,share and comment down bellow .

thank you.

@Hibernate, @WebDevelopmet, ORM ,@java, @MySQL,@LazyLoading,@EgarLoading,@GeneralProgramming

0
Subscribe to my newsletter

Read articles from subhajit chakraborty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

subhajit chakraborty
subhajit chakraborty