• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

get and load in Hibernate

June 6, 2015 By j2eereference Leave a Comment

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.

Related Posts

  • query by example in hibernate
  • How to call stored procedure in Hibernate
  • projection in hibernate
  • Hibernate NamedNative Query
  • Hibernate Named Query
  • Setting up Hibernate
  • Why Hibernate
  • Hibernate Query cache
  • Second level caching in Hibernate
  • First level caching in Hibernate

Filed Under: Hibernate

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.