Hibernate Tutorial

Core Hibernate

Hibernate Mapping

Hibernate Annotations

Hibernate with Spring Framework

Hibernate with Database

Hibernate Log4j

Inheritance Mapping

Hibernate - get() and load() Method

In Hibernate, both get() and load() methods are used to retrieve an object from the database, but they differ in the way they operate. Let's dive into a tutorial to understand the differences between get() and load() methods:

1. get() Method

The get() method belongs to the Session class and is used to fetch an entity based on its primary key. If the entity isn't found, it will return null.

Syntax:

public Object get(Class theClass, Serializable id) throws HibernateException;

Usage:

Session session = sessionFactory.openSession();
Student student = (Student) session.get(Student.class, studentId);

Characteristics:

  • Returns null if the object is not found.
  • Hits the database immediately.
  • The returned object is real, not a proxy.

2. load() Method

The load() method also belongs to the Session class and retrieves an entity based on its primary key. However, it will throw an ObjectNotFoundException if the entity isn't found. It's designed to work with proxies and might not hit the database immediately.

Syntax:

public Object load(Class theClass, Serializable id) throws HibernateException;

Usage:

Session session = sessionFactory.openSession();
Student student = (Student) session.load(Student.class, studentId);

Characteristics:

  • Throws ObjectNotFoundException if the object is not found.
  • May not hit the database immediately; it can return a proxy. The database hit will occur once a method on the loaded object is invoked (lazy initialization).
  • If you're certain that the object exists and don't want to hit the database immediately, load() is more efficient.

Comparison

  1. Return Value:

    • get(): Returns null if object is not found.
    • load(): Throws an ObjectNotFoundException if object is not found.
  2. Database Hit:

    • get(): Hits the database immediately.
    • load(): Returns a proxy by default and hits the database when the first method is accessed on the proxy object.
  3. Performance:

    • get(): Less efficient if you are sure the entity exists and you'll access it later due to immediate database hit.
    • load(): More efficient in such cases because it leverages lazy loading.

Example Scenario:

Let's assume you have a Student entity, and you're looking to retrieve a student with ID 12345.

  • If you use get() and the student doesn't exist, it will return null, and you'll know immediately.
  • If you use load(), Hibernate might just return a proxy object without hitting the database. If you try to access a property on this student, and it turns out it doesn't exist, only then you'll get an ObjectNotFoundException.

In conclusion, you'd typically use get() when you're not sure if an entity exists in the database and want a quick answer. On the other hand, if you're certain an entity exists and want to optimize performance using lazy loading, you'd use load().

  1. Hibernate get() vs load() example:

    • Description: Both get() and load() are used to retrieve entities by their primary key. The key difference lies in when the actual database query is executed. get() fetches the entity immediately, while load() fetches it lazily, meaning the actual database query is deferred until the entity is accessed.
    • Code:
      Session session = sessionFactory.openSession();
      
      // Using get()
      Employee employeeGet = session.get(Employee.class, 1L);
      
      // Using load()
      Employee employeeLoad = session.load(Employee.class, 1L);
      
      // Accessing properties to trigger load
      System.out.println("Employee Name (get()): " + employeeGet.getName());
      System.out.println("Employee Name (load()): " + employeeLoad.getName());
      
      session.close();
      
  2. Lazy loading with Hibernate load() method:

    • Description: The load() method in Hibernate allows lazy loading, meaning the actual database query is deferred until the accessed properties are needed. This is beneficial for performance if not all properties are immediately required.
    • Code:
      Session session = sessionFactory.openSession();
      Employee employee = session.load(Employee.class, 1L);
      
      // No database query is executed at this point
      
      // Accessing properties triggers the database query
      System.out.println("Employee Name: " + employee.getName());
      
      session.close();
      
  3. Using get() and load() for entity retrieval in Hibernate:

    • Description: Both get() and load() methods are used for retrieving entities by their primary key. get() fetches the entity immediately, while load() fetches it lazily.
    • Code:
      Session session = sessionFactory.openSession();
      
      // Using get()
      Employee employeeGet = session.get(Employee.class, 1L);
      
      // Using load()
      Employee employeeLoad = session.load(Employee.class, 1L);
      
      session.close();