Configuring Spring Data JPA with Spring Boot

Take your skills to the next level!

The Persistence Hub is the place to be for every Java developer. It gives you access to all my premium video courses, monthly Java Persistence News, monthly coding problems, and regular expert sessions.

Before Spring Boot, you had to do quite a few things to set up Spring Data JPA. You not only had to annotate your entity classes with mapping annotations, add a dependency to Spring Data JPA and configure your database connection. You also had to enable repositories and transaction management and configure your EntityManagerFactory. That was an annoying and repetitive task.

Spring Boot changes all of that by providing ready-to-use integrations that include the required dependencies and a huge set of default configurations. But that doesn’t mean that you can’t override it if you need to.

In this article, I will explain Spring Boot’s default configuration for Spring Data JPA, which configuration parameters you can use to change it and the configuration you might want to add.

Required Dependencies

Before you can start configuring Spring Data JPA, you need to add it to your application. In a Spring Boot application, that usually means that you need to add the right starter to your project’s dependencies. The easiest way to do that for a new project is to use the Spring Initializr to set up your build process and add all required dependencies. For all existing Spring Boot projects, you need to add the spring-boot-starter-data-jpa module.


You also need to add a dependency to a database-specific JDBC driver. In the following examples, I will connect to a PostgreSQL database and, therefore, need a dependency on PostgreSQL’s JDBC driver.


Default Configuration

As mentioned earlier, Spring Boot’s integration of Spring Data JPA provides an extensive default configuration and adds most of the required dependencies to your project. This includes:

  • a dependency on the HikariCP connection pool and a basic default configuration. You can set all of HikariCP’s configuration parameters in your file by adding the prefix spring.datasource.hikari to the parameter name.
  • the creation of an H2, HSQL, or Derby in-memory database if your classpath contains the corresponding JDBC driver.
  • a dependency on Hibernate as your JPA implementation and the required configuration to instantiate an EntityManagerFactory.
  • a dependency and the required configuration to manage your transactions using an Atomikos embedded transaction manager.
  • the required configuration to use Spring Data JPA’s repositories.

Side Note: Because Spring Data JPA uses Hibernate as its JPA implementation, you can use everything you’ve learned about Hibernate here on the blog with Spring Data JPA.

As you can see, that’s basically everything you previously had to configure in your configuration class. That’s why most projects prefer using Spring Boot over classical Spring.

If you’re using an in-memory database, you don’t need to provide any custom configuration. Spring Boot’s default configuration is usually good enough for all small to mid-sized applications.

Most enterprise applications use a standalone database, e.g., a PostgreSQL or Oracle database server. In that case, you only need to provide the URL, user name, and password to connect to that database. You can do that by setting the following 3 configuration properties in your file.


Customizing the Default Configuration

Just because Spring Data JPA automatically integrates several other projects and configures them for you, you’re not forced to use them. You can easily change the default behavior and integration by providing different dependencies and adding a few parameters to your configuration.

Using a Different Connection Pool

By default, Spring Boot adds a dependency to HikariCP and configures it. You can use a different connection pool by excluding HikariCP from your project dependencies.


Spring Boot then tries to find the following connection pooling implementations in the described order on the classpath and uses the first one it finds:

  • Tomcat Connection Pooling,
  • Commons DBCP2,
  • Oracle UCP.

If you don’t want to rely on a scan of your application classpath, you can also specify the connection pool explicitly by configuring the spring.datasource.type property.


After you changed the connection pool, you can set all of its standard configuration parameters in the file by adding the prefix spring.datasource.tomcat, spring.datasource.dbcp2, or spring.datasource.oracleucp to the parameter name.

Using Bitronix Transaction Manager

In the past, Bitronix has been a popular transaction manager in Spring applications. Spring Boot’s support for it has been deprecated and will be removed in the future.

If you still want to use it, you can add a dependency to spring-boot-starter-jta-bitronix to your application. Spring Boot will then include all required dependencies and configure Bitronix to manage your transaction.

Deactivating Spring Data JPA’s Repositories

I recommend using Spring Data JPA’s repositories. They make the implementation of your persistence much easier by providing a set of standard methods to persist, read and delete entities. They also provide features like derived and custom queries.

If you decide not to use these features, you can deactivate all JPA repositories in your configuration.

Additional Configuration Parameter You Should Know

You can use a huge set of configuration parameters to adapt the behavior of Spring Boot and the libraries you’re using. You can find a complete list of all supported parameters in the official Spring documentation. In the following sections, I explain a few of the parameters you need to know.

Configuring Logging

As explained in my Hibernate Logging Guide, I recommend using 2 different logging configurations for development and production. That, of course, doesn’t change when using Spring Data JPA. Using Spring Boot, you can configure the log levels of all of Hibernate’s categories in your file by adding the prefix logging.level to the name of the log category.

Development Configuration

During development, you want to get as much information about your database interactions as possible. That enables you to understand how your application interacts with the database and find performance issues before deploying them to production.

To get all the required information, I recommend using the following configuration.

This activates Hibernate’s statistics component. It provides you a summary of the number and the time Hibernate spent executing the most important operations during each session. It also adds all executed SQL statements to the log file and shows you how Hibernate used the 2nd level cache.

Spring Boot also supports the parameter to enable the logging of SQL statements. But you should better avoid it because it ignores your logging framework and writes the SQL statements directly to standard out.

Production Configuration

In production, you should set Hibernate’s log level to ERROR to keep the overhead as small as possible.

Configuring JPA and Hibernate Properties

When you’re using JPA and Hibernate without Spring Data JPA, you usually configure it using a persistence.xml file. Within the properties element of that XML file, you can provide vendor-specific configuration parameters.

You can set all of these parameters in your file by adding the prefix to the configuration property’s name.

Configuring Database Creation

By default, Spring Boot automatically creates in-memory databases for you. This is deactivated for all other databases. You can activate it by setting the property spring.jpa.hibernate.ddl-auto to none, validate, update, or create-drop.


I recommend using Spring Boot’s Flyway or Liquibase integration instead. They are more powerful and give you full control over the definition of your table model.


Spring Boot’s starter for Spring Data JPA adds the most common dependencies and a reasonable default configuration to your application. The only thing you need to add is the connection information for your database.

But that doesn’t mean that you have to use these defaults. As you learned in this article, you can easily replace all of the default dependencies and adjust the default configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.