Database Migration with Liquibase, HikariCP, Hibernate and JPA
Java, Maven, Hibernate, JPA, HikariCP, Liquibase
Liquibase
is source control for your database.Liquibase
is an open source database-independent library for tracking, managing and applying database schema changes.
Introduction
This is the third part of a series
of posts focused on Hibernate
and JPA
. In this tutorial we are going to look at database migrations with Liquibase
.
When implementing and deploying a new version of an application, simple and fast refactoring of your database model is one of the most
important things in order to implement flexible business requirements. Liquibase supports tracking, managing and applying database
schema changes.
Prerequisites
Project Structure
This is a build up on previous posts and our folder structure will remain relatively the same:
.
|__src/
| |__main/
| | |__java/
| | | |__com/
| | | | |__tutorial/
| | | | | |__Application.java
| | | | | |__entity/
| | | | | | |__Person.java
| | | | | |__repository/
| | | | | | |__PersonRepository.java
| | | | | | |__PersonRepositoryImpl.java
| | |__resources/
| | | |__META-INF/
| | | | |__persistence.xml
| | | |__dbChangelog.xml
| | | |__log4j2.properties
|__pom.xml
Setting up Dependencies
To add the Liquibase dependency to the project add the following dependency by modifying the pom.xml
file:
JPA Configuration
The next change will be our persistence.xml
file.
file: src/main/resources/META-INF/persistence.xml
:
The following properties have been modified in the persistence.xml
file
-
javax.persistence.schema-generation.database.action
value set tonone
(Liquibase will take care of creating database) -
javax.persistence.jdbc.url
value set tojdbc:h2:file:./target/test;DB_CLOSE_DELAY=-1;MVCC=true
(Will also work with previous value) -
hibernate.connection.handling_mode
this property is added for performance reasons. This tells hibernate to release connection back into pool after use.
Creating the Changelog File
Liquibase database changelog file is where all database changes are listed. Liquibase supports
XML
, YAML
, JSON
and SQL
as formats for Changelog
files. Beyond these built-in formats, the Liquibase extension system
allows you to create changelog files in whatever format you like. This makes it highly flexible.
For this tutorial we will use the XML
changelog format.
file: src/main/resources/dbChangelog.xml
:
The above is self explanatory. The createSequence
is needed so Hibernate knows how to autogenerate the primary key sequence.
Now that we have setup our changelog, we program our application to run migrations each time the application is started.
file: src/main/java/com/tutorial/Application.java
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
private static void init(EntityManager em) {
Connection connection = em.unwrap(SessionImpl.class).connection();
Database database = null;
Liquibase liquibase = null;
try {
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
liquibase = new Liquibase("dbChangelog.xml", new ClassLoaderResourceAccessor(), database);
liquibase.update("test");
} catch (LiquibaseException e) {
e.printStackTrace();
}
}
On line 4
we get the connection created by the EntityManager
and use it to create the Liquibase Database object. Next on line 11
we start the migration by calling the update()
method.
Putting it Together
Finally let us put it all together and run the application.
file: src/main/java/com/tutorial/Application.java
:
Conclusion
In this post we implemented database migrations using Liquibase. We created a database changelog file with our changes. We also learnt
how to run these changes programmatically.
As usual you can find the full example to this guide in the github repository. Until the next post, keep doing cool things .