May 8 2015

Difference between get and load in hibernate

Difference between get and load in hibernate

Hibernate Session provides get and load methods to retrieve the persistent objects from database by identifier.

Object load(Class theClass,Serializable id)  throws HibernateException

Object get(Class clazz,Serializable id) throws HibernateException

Following are the differences between load and get method in hibernate

  1. If load method cannot find the object in the cache or database, an ObjectNotFoundException is thrown whereas get method returns null if it is not able to find the object.

  1. Load method returns a proxy object that will be initialized or loaded when it is accessed for the first time whereas get method hits the database and returns the persistent object.
  1. If you are sure that object exists load method can be used otherwise get method can be used.

Example : Student.java

package com.answersz.dao;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student")
public class Student {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "STUDENT_ID")
 private Integer studentId;

 @Column(name = "STUDENT_NAME")
 private String studentName;

 @Column(name = "FATHER_NAME")
 private String fatherName;

 @Column(name = "DATE_OF_BIRTH")
 private Date dateofBirth;

 @Column(name = "COURSE")
 private String course;

 // getters and setters

}

Table select * from student;

STUDENT_ID STUDENT_NAME FATHER_NAME DATE_OF_BIRTH COURSE
950 JOSEPH GEORGE 4/30/2015 MTECH

Session save Example

Student object with identifier 950 exist in the database get method returns the persistent object whereas Student object with identifier 951 does not exist get method returns null.

SessionGetExample.java


package com.answersz.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

// Difference between get and load in hibernate
public class SessionGetExample {

public static void main(String[] args) {

SessionFactory factory = new AnnotationConfiguration().configure()
 .buildSessionFactory();
 Session session = factory.openSession();

Transaction t = session.beginTransaction();

Student student1 = (Student) session.get(Student.class, 950);
 if (student1 != null) {
 System.out.println("----- Student 1 --------");
 System.out.println("Student ID : " + student1.getStudentId());
 System.out.println("Student Name : " + student1.getStudentName());
 }

Student student2 = (Student) session.get(Student.class, 951);
 if (student2 != null) {
 System.out.println("----- Student 2 --------");
 System.out.println("Student ID : " + student2.getStudentId());
 System.out.println("Student Name : " + student2.getStudentName());
 }

t.commit();

session.close();
 factory.close();
 }
}

Output

Hibernate: select student0_.STUDENT_ID as STUDENT1_0_0_, student0_.COURSE as COURSE0_0_, student0_.DATE_OF_BIRTH as DATE3_0_0_, student0_.FATHER_NAME as FATHER4_0_0_, student0_.STUDENT_NAME as STUDENT5_0_0_ from student student0_ where student0_.STUDENT_ID=?
----- Student 1 --------
Student ID : 950
Student Name : JOSEPH
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_0_, student0_.COURSE as COURSE0_0_, student0_.DATE_OF_BIRTH as DATE3_0_0_, student0_.FATHER_NAME as FATHER4_0_0_, student0_.STUDENT_NAME as STUDENT5_0_0_ from student student0_ where student0_.STUDENT_ID=?

Session Load Example

In the below example, load method returns proxy objects for both Student objects with identifiers 950 and 951. Whenever student1 object is accessed, the object will be loaded from database .Whenever you are trying to access student2 which does not exist in the database, ObjectNotFoundException will be thrown

SessionLoadExample.java


package com.answersz.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

// Difference between get and load in hibernate
public class SessionLoadExample {

public static void main(String[] args) {
SessionFactory factory = new AnnotationConfiguration().configure()
.buildSessionFactory();
Session session = factory.openSession();

Transaction t = session.beginTransaction();

Student student1 = (Student) session.load(Student.class, 950);
if (student1 != null) {
System.out.println("----- Student 1 --------");
System.out.println("Student ID : " + student1.getStudentId());
System.out.println("Student Name : " + student1.getStudentName());
}

Student student2 = (Student) session.load(Student.class, 951);
if (student2 != null) {
System.out.println("----- Student 2 --------");
System.out.println("Student ID : " + student2.getStudentId());
System.out.println("Student Name : " + student2.getStudentName());
}

t.commit();

session.close();
factory.close();
}
}

Output

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.answersz.dao.Student#951]


----- Student 1 --------
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_0_, student0_.COURSE as COURSE0_0_, student0_.DATE_OF_BIRTH as DATE3_0_0_, student0_.FATHER_NAME as FATHER4_0_0_, student0_.STUDENT_NAME as STUDENT5_0_0_ from student student0_ where student0_.STUDENT_ID=?
Student ID : 950
Student Name : JOSEPH
----- Student 2 --------
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_0_, student0_.COURSE as COURSE0_0_, student0_.DATE_OF_BIRTH as DATE3_0_0_, student0_.FATHER_NAME as FATHER4_0_0_, student0_.STUDENT_NAME as STUDENT5_0_0_ from student student0_ where student0_.STUDENT_ID=?
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.answersz.dao.Student#951]
at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:375)
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:172)
at com.answersz.dao.Student$$EnhancerByCGLIB$$dd86bac8.getStudentId(<generated>)
at com.answersz.dao.SessionLoadExample.main(SessionLoadExample.java:28)


Copyright © 2014. All rights reserved.

Posted May 8, 2015 by answersz in category Hibernate

Leave a Comment

Your email address will not be published. Required fields are marked *

*