What is difference between Hibernate Session get() and load() method

Difference between Hibernate Session get() and load() method

Hibernate session comes with different methods to load data from the database. get and load is the most used method, at first look they seem similar but there are some differences between them.
  1. get() loads the data as soon as it’s called whereas load() returns a proxy object and loads data only when it’s actually required, so load() is better because it supports lazy loading.
  2. Since load() throws an exception when data is not found, we should use it only when we know data exists.
  3. We should use get() when we want to make sure data exists in the database.
From JavaDoc:

Session.get() → This method returns a persistence object of the given class with the given identifier. It will return null if there is no persistence object.

Session.load() → This method returns a persistence object of the given class with the given identifier. It will throw an exception ObjectNotFoundException if an entity does not exist in the database. The load() method may return a proxy object instead of a real persistence object.

Usage of the Hibernate Session load() method

import org.hibernate.Session;

import net.javaguides.hibernate.entity.Student;
import net.javaguides.hibernate.util.HibernateUtil;

public class App {
    public static void main(String[] args) {

        Integer studentId = 1;
        /************************** Save Entity ***************************/
        Session sessionOne = HibernateUtil.getSessionFactory().openSession();
        sessionOne.beginTransaction();

        // create new student object
        Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");

        // save student object to database
        sessionOne.save(student);
        sessionOne.getTransaction().commit();

        /*******************************************************************/

        //Let's open a new session to test load() methods
        Session sessionTwo = HibernateUtil.getSessionFactory().openSession();
        sessionTwo.beginTransaction();

        //first load() method example
        Student student1 = (Student) sessionTwo.load(Student.class, studentId);
        System.out.println(student1.getFirstName() + " - " + student1.getLastName());

        //Let's verify the entity name
        System.out.println(sessionTwo.getEntityName(student1));

        sessionTwo.getTransaction().commit();
    }
}
Check out a complete example at https://www.javaguides.net/2018/11/hibernate-5-load-method-example.html.

Usage of the Hibernate Session get() method

public void getStudent(int id) {
        Transaction transaction = null;
        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            // start a transaction
            transaction = session.beginTransaction();

            // get Student entity using get() method
            Student student = session.get(Student.class, id);
            System.out.println(student.getFirstName());
            System.out.println(student.getEmail());

            // commit transaction
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

Checkout the complete example at https://www.javaguides.net/2018/11/hibernate-get-load-and-byid-method-examples.html.

Reference




Comments