Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:


1. Overview

Spring Data provides convenient methods for retrieving entities from a data store, including findById and getById. Although they may seem similar at first glance, there are subtle differences that can impact our code’s functionality.

This tutorial will explore these differences and help us determine when to use each method effectively.

2. Understanding findById

First, let’s take a look at the findById method.

2.1. Method Signature

The findById method is defined in the CrudRepository interface:

Optional<T> findById(ID id);

2.2. Behavior and Return Type

The findById method retrieves an entity by its unique identifier (id). It returns an Optional wrapper, indicating that the entity may or may not exist in the data store. If the entity is found, it will be wrapped inside the Optional. Otherwise, the Optional will be empty.

2.3. Use Cases

There are a couple of use cases where findById is the better choice.

The first is when we expect the entity to be potentially absent in the data store and want to handle both scenarios (found and not found) gracefully.

Additionally, it’s handy when we want to use it in combination with the Java 8 Optional API to perform conditional operations or execute fallback logic when the entity is not found

3. Exploring getById

Next, let’s explore the getById method.

3.1. Method Signature

The JpaRepository interface defines the getById method:

T getById(ID id);

3.2. Behavior and Return Type

Unlike findById, the getById method returns the entity directly instead of wrapping it in an Optional. If the entity doesn’t exist in the data store, an EntityNotFoundException will be thrown.

3.3. Use Cases

We can use getById when we’re sure the entity with the given id exists in the data store. This method also offers a more concise syntax and avoids the need for additional Optional handling.

4. Choosing the Right Method

Finally, let’s consider a few factors that can help us to determine whether to use findById or getById:

  • Existence guarantee: If the entity’s existence is guaranteed, and there would be an exceptional situation if missing, prefer getById. This approach avoids unnecessary Optional handling and simplifies the code.
  • Potential absence: If the entity’s presence is uncertain, or we need to handle both found and not found scenarios gracefully, use findById along with the Optional API. This approach allows us to perform conditional operations without throwing exceptions.
  • From version 2.7 of Spring Boot, the getById method is marked as deprecated, and the documentation recommends using the getReferenceById method instead.

5. Conclusion

In this tutorial, we’ve learned the differences between findById and getById methods in Spring Data. While findById returns an Optional and gracefully handles the absence of an entity, getById directly returns the entity and throws an exception if it doesn’t exist. The choice between the two methods depends on whether the entity’s existence is guaranteed or uncertain and whether exception handling or conditional operations are required.

So we can choose the one that best aligns with our application’s logic and requirements.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Comments are closed on this article!