Introduction to Spring Boot Starter Data JPA

In Spring Boot applications that work with relational databases, a very common dependency you often use is Spring Boot Starter Data JPA. With this dependency alone, you can use Spring Data JPA, Hibernate, and many features related to Jakarta Persistence without having to manually configure too many components. So what does Spring Boot Starter Data JPA actually provide? What components does Spring Boot auto-configure for this Starter? And what happens when a Spring Boot application starts? In this article, we will explore Spring Boot Starter Data JPA from basic concepts to the auto-configuration mechanism within Spring Boot!

First, you need to distinguish between some commonly confused concepts:

  • Spring Boot Starter
  • Spring Data JPA
  • Jakarta Persistence
  • Hibernate

These components are related to each other, but they are not the same thing.

You can imagine it simply like this:

Regarding Spring Boot Starter, in the previous tutorial, I introduced its purpose: a dependency descriptor that helps us easily declare a group of dependencies necessary for a specific function. Spring Boot Starter Data JPA is a Spring Boot Starter designed for working with relational databases using Spring Data JPA.

You can create a new Spring Boot project using Starter Data JPA along with a JDBC driver, such as the MySQL JDBC driver, as follows:

Result:

Check the pom.xml file of this project, and you will see that the Spring Boot Starter Data JPA dependency is declared as follows:

Maven will load spring-boot-starter-data-jpa along with the dependencies that Starter requires, as follows:


Spring Boot Starter Data JPA will manage the dependencies and corresponding versions of the libraries:

  • Spring Boot JPA
  • Spring Data JPA
  • Spring ORM
  • Jakarta Persistence
  • Hibernate
  • Transaction infrastructure

through Spring Boot’s dependency management.

Now let’s look at the role of each dependency!

Spring Boot JPA

Spring Boot JPA provides Jakarta Persistence support classes, auto-configuration mechanisms, and integration with Spring Boot. I will have a more detailed article about the auto-configuration and integration of Spring Boot Starter Data JPA with Spring Boot later.

Spring Data JPA

Spring Data JPA provides abstractions that make working with Jakarta Persistence easier.

For example, for the StudentRepository interface:

You don’t need to write your own implementation for the StudentRepository interface. Spring Data JPA will create the appropriate repository implementation/proxy during application startup, allowing you to easily interact with the database without writing much code.

Spring ORM

The Spring ORM will be responsible for integrating Jakarta Persistence with the Spring Framework. The LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean classes are both located within this module.

Jakarta Persistence

Jakarta Persistence, formerly known as JPA, defines the API and specification for persistence in Java.

For example, you can define a Student entity as follows:

Annotations such as:

  • @Entity
  • @Id
  • @GeneratedValue
  • @OneToMany
  • @ManyToOne

from the Jakarta Persistence API.

Hibernate

Hibernate is a popular implementation of Jakarta Persistence. Simply put:

  • Jakarta Persistence is the API/specification.
  • Hibernate is the implementation of this specification.

Spring Boot Starter Data JPA uses Hibernate as the default implementation of Jakarta Persistence.

Transaction infrastructure

Spring Boot Starter Data JPA provides the necessary modules and configurations to implement transaction management in your application. For example:

  • spring-tx → provides abstractions for transactions.
  • spring-orm → integrates Jakarta Persistence/ORM with the Spring Framework.
  • JpaTransactionManager → manages Jakarta Persistence transactions using EntityManager.
  • @Transactional → configures Spring to know whether or not to apply transactions to a method or all methods in a particular class.

For a clearer understanding of the differences between Spring Boot Starter Data JPA and Spring Data JPA, please refer to the following summary table:

Feature Spring Data JPA Spring Boot Starter Data JPA
Repository abstraction Yes Use Spring Data JPA
JpaRepository Yes Use Spring Data JPA
Hibernate dependency Not main purpose Provided
Spring Boot auto-configuration No Yes
DataSource auto-configuration No Yes
JPA infrastructure configuration Manual configuration Auto-configured by Spring Boot
Requires Spring Boot No Yes

Therefore, you can understand that Spring Boot Starter Data JPA is not a new persistence technology. It’s a Spring Boot Starter that helps us use Spring Data JPA with JPA/Hibernate and the necessary infrastructure more conveniently.

Now, let’s try converting the example project we made in the previous article to use Spring Boot Starter Data JPA.

For Jakarta Persistence to work with the database, our application will need DataSource information. In Spring Boot, you can configure it as follows:

With this declaration, when running the Spring Boot application, the Spring Boot JDBC DataSourceAutoConfiguration class can detect the necessary dependencies and configurations to create that DataSource.

I will copy the Student entity class and the repository class for this entity, StudentRepository, to the example project in this tutorial.

Spring Boot needs to detect the Student entity so that Hibernate can use it in its persistence unit.

Typically, Spring Boot uses the application class’s package and subpackages—in my example project, SpringBootStarterDataJpaExampleApplication—as the basis for component scanning and related scanning mechanisms. With the package structure in my example project, Spring Boot can automatically scan to include the Student entity.

In cases with special package structures, you can use the @EntityScan annotation to specify the package containing the entity information! I will discuss this mechanism in detail in a later article.

Regarding StudentRepository, Spring Boot uses the DataJpaRepositoriesAutoConfiguration class in the Spring Boot JPA module to automatically configure the Spring Data JPA repository. Similar to entity classes, Spring Boot also uses the application class’s package and subpackages as the basis for component scanning, automatically detecting the defined Repository classes. If you want Spring Boot to scan these Repository classes in a different package, you can also use the @EnableJpaRepositories annotation. I will discuss this mechanism in detail in a later article!

A very important component of Jakarta Persistence is the EntityManagerFactory. EntityManagerFactory is used to create EntityManager. In Spring Boot applications, we usually don’t need to create EntityManagerFactory ourselves.

Spring Boot will auto-configure the Jakarta Persistence infrastructure based on:

  • DataSource
  • JPA dependencies
  • Hibernate
  • application properties
  • entity classes
  • Other Spring configuration

You can also inject EntityManager if you want. This allows the application to directly use the Jakarta Persistence API in addition to Spring Data JPA repositories.

To run this example application, you can add a bean of the CommandLineRunner class as follows:

Results when running the application:

As you can see, with Spring Boot applications, we don’t need to write as much code as when using Spring Data JPA, yet we still achieve the desired results.

The Spring Data JPA module provides many properties to configure Jakarta Persistence. For example:

With this configuration, when running the application, you can easily observe the SQL generated by Hibernate. For example:

Another property that is commonly used:

This property determines how Hibernate handles the database schema.

Some common values:

  • none
  • validate
  • update
  • create
  • create-drop

However, you need to be especially careful when using this property in a production environment. In a production environment, database schema management should generally be done using database migration tools like Flyway or Liquibase instead of relying on Hibernate schema generation.

In short, Spring Boot Starter Data JPA is one of the most commonly used Spring Boot starters in applications working with relational databases.

The most important thing to understand is that this Starter is not a standalone persistence framework. Instead, it combines many components related to Spring Boot Data JPA, Jakarta Persistence, Hibernate, and Spring Boot’s auto-configuration mechanism, making it easier to work with databases in Spring Boot applications.

Spring Boot Starter Data JPA simplifies the integration of Spring Boot Data JPA, Jakarta Persistence, and Hibernate into Spring Boot applications through dependency management and auto-configuration.

However, using Jakarta Persistence effectively still requires us to understand entity mapping, transactions, SQL, database design, query performance, and Hibernate behavior.

Add Comment