Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database.

Session.get()

The session.get() method is used to fetch a persistent object of the given class with the given identifier. When we use the session.get() method, it will directly hit the database and return the real object from the database. If no persistent object found in the database, it returns null.

There are four get() methods available in Session interface with different parameters:

  • get(Class class, Serializable id)
  • get(Class class, Serializable id, LockOptions lockOptions)
  • get(String entityName, Serializable id)
  • get(String entityName, Serializable id, LockOptions lockoptions)

All of the above methods perform the same functions.

How to use session.get() method

Session session = SessionFactory.getCurrentSession();
Employee employee = (Employee) session.get(Employee.class, 1); 

In the above code, session.get() method is fetching an object of Employee class with the given identifier 1.

Session.load()

The session.load() method returns a 'proxy' object instead of a real object, without hitting the database. A proxy is an object with the given identifier value, and its properties are not initialized. It acts as a temporary object. If the persistent object is not found, it will throw an exception called ObjectNotFoundException.

There are four load() methods available in the Session interface with different parameters:

  • load(Class theClass, Serializable id)
  • load(Class theClass, Serializable id, LockOptions lockOptions)
  • load(String entityName, Serializable id)
  • load(String entityName, Serializable id, LockOptions lockOptions)

All of the above methods perform the same functions.

How to use session.load() method

Session session = SessionFactory.getCurrentSession();
Employee employee = (Employee) session.load(Employee.class, 1); 

In the above code, session.load() is fetching an object of Employee class with the given identifier 1.

Difference between get() and load()

            Parameter                        Get                      Load
Use If we are not confident about the presence of the object, we use the get() method. If we are confident about the existence of the object, we can use the load() method.
Database hit The get() method always hits the database when called. The load() method does not always hit the database when called.
Object not found The get() method will return null if the object is not found in the database. The load() method will throw an exception (ObjectNotFoundException) if the object is not found.
Object retrieval This method will return a real object. This method will return a proxy object in place of a real object.
Loading type The get() method uses Eager Loading. The load() method uses Lazy Loading.

Related Topics

Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

5 minutes read.

Hibernate Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 minutes read.

Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to...

6 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Hibernate GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

3 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate Composite Primary Key

Hibernate Composite Primary Key A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is...

8 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

2 minutes read.

Hibernate Caching

What is Caching? Caching is a process of saving data into the cache memory. A cache is a temporary storage layer which is used to increase the speed of data access. It allows...

2 minutes read.

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Hibernate save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

3 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 minutes read.

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 minutes read.

Hibernate vs. JDBC

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

2 minutes read.

Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of the...

2 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.