Liquibase is source control for your database. Liquibase is an open source database-independent
library for tracking, managing and applying database schema changes.
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
To add the Liquibase dependency to the project add the following dependency by modifying the pom.xml file:
The next change will be our persistence.xml file.
The following properties have been modified in the persistence.xml file
javax.persistence.schema-generation.database.action value set to none (Liquibase will take care of creating database)
javax.persistence.jdbc.url value set to jdbc: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.
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.
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 .