Last updated: 2024-07-05
Loading initial data in Spring Boot
We have already initialized the database schema using Hibernate, Flyway or Liquibase. However, in addition to the schema, we often require certain data that is necessary for the application during runtime. This could be e.g. a table Country
, in which the ISO code and a currency is stored. Which options do we have to initialize this data in a Spring Boot application?
Using an ApplicationRunner
An ApplicationRunner
is executed immediately after the start of our Spring Boot application. If there are multiple ApplicationRunners
in the application, they can also be sorted in the desired order using @Order
.
Using an ApplicationRunner to initialize data
This option provides the highest flexibility, as the process can be controlled directly in our code.
Using the data.sql
If we are working with a relational database, we could simply place a data.sql
in our resources
folder. This script will be automatically executed by Spring Boot against the configured DataSource
during startup.
Special insert script for PostgreSQL
We have to ensure that the values are not created multiple times. If our database schema is created by Hibernate, we should also add the following property to run our script only after Hibernate made its changes.
Running the data.sql after Hibernate
Using changelogs
If we have chosen Flyway or Liquibase for schema generation, we can also use them for loading our initial data. They implicitly ensure that the changes are executed exactly once against the connected database.
In case of Liquibase we simply add another changelog with a higher timestamp into our changelogs
folder.
Liquibase changelog in resources/changelogs/2023-01-18_11-00.yml
In case of Flyway we create our migration script directly in the dialect of the used database. We store it in resources/db/migration/V002__INITIAL_COUNTRIES.sql
so that it is executed immediately after the table structure has been created.
Flyway migration script
All three ways are valid options to initialize our data - so choose according to your own preference. To avoid confusion for the developers, the parallel use of multiple approaches should be avoided.