Partner – JPA Buddy – NPI (tag=JPA/Hibernate)
announcement - icon

The right tools can and will save a lot of time. As long as you are using Hibernate and IntelliJ IDEA you can boost your coding speed and quality with JPA Buddy. It will help in a lot of the day-to-day work:

  • Creating JPA entities that follow best practices for efficient mapping
  • Creating DTOs from entities and MapStruct mappers using convenient visual tools
  • Generating entities from the existing database or Swagger-generated POJOs
  • Visually composing methods for Spring Data JPA repositories
  • Generating differential SQL to update your schema in accordance with your changes in entities
  • Autogenerating Flyway migrations and Liquibase changelogs comparing entities with the database or two databases
  • … and a lot more

Simply put, you'll learn and use the best practices of Hibernate and surrounding technology and become a lot more!

Definitely visit the JPA Buddy site to see its features in action closer.

Course – LSD (cat=Persistence)

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


1. Overview

In this tutorial, we'll learn how to start a Spring Boot application without having a running database.

By default, if we have a Spring Boot application that contains Spring Data JPA, then the application will automatically look to create a database connection. However, it may be required to avoid this in situations where a database is not available when the application is started.

2. Setup

We'll work with a simple Spring Boot application that uses MySQL. Let's look at the steps to set up the application.

2.1. Dependencies

Let's add the Spring Data JPA starter and MySql Connector dependencies to the pom.xml file:



This adds JPA, MySQL Connector, and Hibernate to the classpath.

In addition to this, we want a task to keep running when the application is started. For this, let's add the Web starter to the pom.xml file:


This starts a web server on port 8080 and keeps the application running.

2.2. Properties

There are some mandatory properties we need in the file to set before starting the application:


Let's understand the properties we set:

  • spring.datasource.url: the URL of the server and the name of the database.
  • spring.datasource.driver-class-name: the driver class name. The MySQL connector provides this driver.
  • We have set this to MySQL 5. This tells the JPA provider to use the MySQL 5 dialect.

In addition to this, we need to set the username and password required to connect to the database:


2.3. Start the Application

If we start the application, we'll see the following error:

HHH000342: Could not obtain connection to query metadata
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

This is because we do not have a database server running at the specified URL. However, the default behavior of the application is to perform these two operations:

  • JPA tries to connect to the database server and fetches the metadata
  • Hibernate will try to create a database if it doesn't exist. This is due to the property spring.jpa.hibernate.ddl-auto set to create by default.

3. Running Without A Database

To continue without a database, we need to fix the default behavior by overriding the two properties mentioned above.

First, let's disable the metadata fetch:

Then, we disable the automatic database creation:


By setting this property, we have disabled the creation of a database. Therefore the application has no reason to create a connection.

Unlike earlier, now, when we start the application, it starts without any errors. Unless an operation requires interaction with the database, a connection will not be initiated.

4. Conclusion

In this article, we've learned how to start a Spring Boot application without requiring a running database.

We looked at the default behavior of the application that looks for a database connection. Then we fixed the default behavior by overriding two properties.

As always, the code examples used in this article can be found over on GitHub.

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!