The difference between get and load method in Hibernate tutorial will show you how to use two method in different circumstances. This tutorial is a one of the most popular question asked in Hibernate and Spring interviews.
Hibernate Session provide different methods to fetch data from database. Two of them are get() and load() method. Both of them look quite similar to each other however there are few difference between load and get() method that can affect performance of application. Let’s look at them with below examples.
Other interesting posts you may like
In this difference between get and load method in Hibernate tutorial, we will create the final project directory structure like below:
Let’s try to fetch data using get() and load() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.javabycode.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.javabycode.hibernate.model.Student; public class HibernateSessionExample { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); // get() method Example System.out.println("----------Start to executing get() method----------"); Student student = (Student) session.get(Student.class, new Long(1)); System.out.println("----------Finish to executing get() method----------"); System.out.println("Student ID= " + student.getId()); System.out.println("Student Get Details:: " + student + "\n"); // load() method Example System.out.println("----------Start to executing load() method----------"); Student student1 = (Student) session.load(Student.class, new Long(2)); System.out.println("----------Finish to executing load() method----------"); System.out.println("Student ID= " + student1.getId()); System.out.println("Student load Details:: " + student1 + "\n"); // Close resources tx.commit(); sessionFactory.close(); } } |
Run above main class and it produces following output.
1 2 3 4 5 6 7 8 9 10 11 |
----------Start to executing get() method---------- Hibernate: select student0_.id as id1_0_0_, student0_.CODE as CODE2_0_0_, student0_.ENTERING_DATE as ENTERING3_0_0_, student0_.NAME as NAME4_0_0_, student0_.NATIONALITY as NATIONAL5_0_0_ from STUDENT student0_ where student0_.id=? ----------Finish to executing get() method---------- Student ID= 1 Student Get Details:: Student [id=1, name=David Pham, enteringDate=2016-10-15 00:00:00.0, nationality=USA, code=12345678] ----------Start to executing load() method---------- ----------Finish to executing load() method---------- Hibernate: select student0_.id as id1_0_0_, student0_.CODE as CODE2_0_0_, student0_.ENTERING_DATE as ENTERING3_0_0_, student0_.NAME as NAME4_0_0_, student0_.NATIONALITY as NATIONAL5_0_0_ from STUDENT student0_ where student0_.id=? Student ID= 2 Student load Details:: Student [id=2, name=Larry Page, enteringDate=2016-10-15 00:00:00.0, nationality=USA, code=12345679] |
As you see on the above output, it’s clear that get() method returns the object by fetching it from database.
While load() method just returns the reference of an object. Only when we access properties of the object it just loads the data from database. It is reason why the load() method can return reference of object that might not actually exists.
Let’s try to fecth data that doesn’t exists with below examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.javabycode.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.javabycode.hibernate.model.Student; public class HibernateSessionExample2 { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); // get() method Example System.out.println("----------Start to executing get() method----------"); Student student = (Student) session.get(Student.class, new Long(10)); System.out .println("----------Finish to executing get() method----------"); if (student != null) { System.out.println("Student ID= " + student.getId()); System.out.println("Student Get Details:: " + student + "\n"); } // load() method Example System.out .println("----------Start to executing load() method----------"); Student student1 = (Student) session.load(Student.class, new Long(20)); System.out .println("----------Finish to executing load() method----------"); if (student1 != null) { System.out.println("Student ID= " + student1.getId()); System.out.println("Student load Details:: " + student1 + "\n"); } // Close resources tx.commit(); sessionFactory.close(); } } |
Run above main class and it produces following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
----------Start to executing get() method---------- Hibernate: select student0_.id as id1_0_0_, student0_.CODE as CODE2_0_0_, student0_.ENTERING_DATE as ENTERING3_0_0_, student0_.NAME as NAME4_0_0_, student0_.NATIONALITY as NATIONAL5_0_0_ from STUDENT student0_ where student0_.id=? ----------Finish to executing get() method---------- ----------Start to executing load() method---------- ----------Finish to executing load() method---------- Hibernate: select student0_.id as id1_0_0_, student0_.CODE as CODE2_0_0_, student0_.ENTERING_DATE as ENTERING3_0_0_, student0_.NAME as NAME4_0_0_, student0_.NATIONALITY as NATIONAL5_0_0_ from STUDENT student0_ where student0_.id=? Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.javabycode.hibernate.model.Student#20] at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:253) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:262) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:176) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) at com.javabycode.hibernate.model.Student_$$_jvstdf2_0.getId(Student_$$_jvstdf2_0.java) at com.javabycode.hibernate.HibernateSessionExample2.main(HibernateSessionExample2.java:34) |
Look into the above output. The get() method returns null. This is evident it try to fetch the data as soon as it’s executed.
While the load() method just try to load object since we print the id of that object. In this case, we try to fetch data that doesn’t exists so that it throws org.hibernate.ObjectNotFoundException.
Conclusion
Based on the above examples we have difference between get and load method in Hibernate like below:
The get() method fetches data as soon as it’s executed while the load() method returns a proxy object and fetches only data when object properties is required.
So that the load() method gets better performance because it support lazy loading.
Whe should use the load() method only when we know data exists because it throws exception when data is not found.
In case we want to make sure data exists we should use the get() method.
In short, you should understand the differential in between, and decide which method is best fix in your application. That’s all for the difference between get and load method in Hibernate tutorial.
Download complete source code, please click link below
HibernateSessionExample-1.zip (402 downloads)