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 Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate first Example

Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:- Create the POJO...

2 minutes read.

Hibernate Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

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 Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

3 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 Query Language (HQL)

Hibernate provides its query language called Hibernate Query Language (HQL). HQL is an object-oriented query language, similar to the native SQL language, and it works with persistent objects. It is a database-independent and...

2 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 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 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 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 Annotation with Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases and it also uses the specifications of the...

2 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 Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 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.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.