get and load in Hibernate
Hibernate session provides two methods – get and load to access the objects. Both are doing same kind of functionality , But there are some differences.
Lets try to understand those differences with example .
1) get method hit the DB immediately . But Load wont hit the DB immediately until we invoke the object
load() method : This returns a proxy object by default and there won’t be any database hit until we invoke it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.j2eereference.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class TestGetLoad { public static void main(String[] args) { AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Employee.class); SessionFactory sessionFactory = config.configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Employee emp=(Employee)session.load(Employee.class, 1); System.out.println("Before invoking the object"); System.out.println("Employee name is "+emp.getEmp_name()); session.close(); } } |
Output:
1 2 3 |
Before invoking the object Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from employee employee0_ where employee0_.emp_id=? Employee name is Jose |
Here you can notice the DB hit happened only after we invoked the object with emp.getEmp_name().
get() method .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.j2eereference.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class TestGetLoad { public static void main(String[] args) { AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Employee.class); SessionFactory sessionFactory = config.configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Employee emp=(Employee)session.get(Employee.class, 1); System.out.println("Before invoking the object"); System.out.println("Employee name is "+emp.getEmp_name()); session.close(); } } |
output:
1 2 3 |
Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from employee employee0_ where employee0_.emp_id=? Before invoking the object Employee name is Rakesh |
Here you can notice that the DB hit happened immediately.
2) load () method will throw exception if there is no record in the DB with given unique ID , But get will return null .
We have an employee table and it has only one record , emp_id is 1 . Now lets try fetching the object with unique emp_id 10 which is not available.
load() method: This method will throw exception when we try to load employee object with unique id 10 , which does not exist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.j2eereference.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class TestGetLoad { public static void main(String[] args) { AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Employee.class); SessionFactory sessionFactory = config.configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Employee emp=(Employee)session.load(Employee.class, 10); System.out.println("emp is "+emp); session.close(); } } |
output:
1 2 3 4 5 6 7 8 9 10 11 |
Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from employee employee0_ where employee0_.emp_id=? <span style="color: red;">Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.j2eereference.hibernate.Employee#10] at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150) at com.j2eereference.hibernate.getLoad.Employee$$EnhancerByCGLIB$$63203938.toString() at java.lang.String.valueOf(Unknown Source) at java.lang.StringBuilder.append(Unknown Source) at com.j2eereference.hibernate.getLoad.TestGetLoad.main(TestGetLoad.java:14) </span> |
get() method: This method will return null when we try to get employee object with unique id 10 , which does not exist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.j2eereference.hibernate.getLoad; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class TestGetLoad { public static void main(String[] args) { AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Employee.class); SessionFactory sessionFactory = config.configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Employee emp=(Employee)session.get(Employee.class, 10); System.out.println("emp is "+emp); session.close(); } } |
output
1 2 |
Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from employee employee0_ where employee0_.emp_id=? emp is null |
Conclusion :
- If you are not sure that the object exists , use get method.
- If you are sure that the object exist, you can use load method to access the objet.
- get method will return a completely initialized object which may involve multiple DB hits based upon object relational mapping.
- load() method returns a proxy which can be initialized on demand.
Leave a Reply