Using Liquibase with Hibernate

Hibernate is an object-relational mapping (ORM) tool that can be used alongside Liquibase to provide a persistent framework for a relational database.

The purpose of this document is to guide you through the process of creating a new Liquibase project and integrating it into your Hibernate ORM setup. In this tutorial, you will learn how to install the required database drivers and configure the file to establish a connection to an H2 database.

Supported Versions

  • 3.X: liquibase-hibernate3
  • 4.0–4.2: liquibase-hibernate4.2
  • 4.3: liquibase-hibernate4
  • 5.0+: liquibase-hibernate5


  • Ensure that you have installed the latest version of Liquibase. Find the appropriate executable for your platform.
  • Ensure that the Liquibase executable location is in the PATH environment variable. For more information, see Installing Liquibase.
  • Note: We will refer to the location of the Liquibase executable as $LIQUIBASE_HOME in this tutorial.

  • Download and install Maven.

Driver Information

Hibernate can be used with several databases that are supported by Liquibase, such as H2.

Supported Commands and Change Types

For information on supported Liquibase Commands and Liquibase Change Types, go to Liquibase Database Tutorials and select the type of database you are using Hibernate with.

Testing Your Connection

To start the H2 server included in the Liquibase distribution, open the command line, go to $LIQUIBASE_HOME/examples, and run liquibase init start-h2. This will open a database console in your browser.

You can check the status of the database by entering create table test_table (id int) in the text area of the database console and selecting Run. You will see TEST_TABLE appear in the object view. For more information, see Using Liquibase with H2 .

Creating a New Liquibase Project with Hibernate

We will be creating a Maven project for this tutorial. To configure a Liquibase project for Hibernate, perform the following steps:

  1. Install the Liquibase extension by copying the extension jar file into the $LIQUIBASE_HOME/lib directory.
  2. Create a project folder anywhere on your machine and name it HibernateLiquibase. The project folder will be used to store the Liquibase configuration files and changelogs.
  3. Create a file and place it in your src/main/resources directory within the HibernateLiquibase folder.
  4. Note: For more information about the Liquibase properties file, see Specifying Properties in a Connection Profile.

    The file should contain the following properties, along with the necessary authentication properties:


    If you do not specify these in your file, you must enter them as attributes in the command line.

    Note: Liquibase supports only the following special characters in passwords: ~ # $ % * ( ) - _ + [ ] { } . ?. Unsupported special characters are as follows: @ & / : < > " ' ` | ^ ! = , \ <spaces>.

    • If you placed your jar files in the liquibase/lib install directory, there is no need to specify the classpath property in the file. Otherwise, put the path to your drivers as it is shown in the previous example.
    • If you have a Liquibase Pro License Key and want to apply it to your project, add the following property to your file:
    • liquibaseProLicenseKey: <paste license key>
  5. Create a pom.xml file in the HibernateLiquibase folder and add the following content to the file:
  6. <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns=""
    	xmlns:xsi="" xsi:schemaLocation="">
    	<name>Liquibase Hibernate Example</name>
    	<description>Demo project for liquibase and hibernate</description>
  7. Get the additional dependencies by running the following command from the same directory as the pom.xml file:
  8. mvn dependency:copy-dependencies -DoutputDirectory=${}/lib -Dhttps.protocols=TLSv1.2

    Copy the following jars from ./target/lib to $LIQUIBASE_HOME/lib:

    • byte-buddy-1.10.10.jar
    • classmate-1.5.1.jar
    • dom4j-2.1.3.jar
    • hibernate-commons-annotations-5.1.0.Final.jar
    • hibernate-core-5.4.21.Final.jar
    • jandex-2.1.3.Final.jar
    • javax.persistence-api-2.2.jar
    • jboss-logging-3.3.2.Final.jar
    • jboss-transaction-api_1.2_spec-1.1.1.Final.jar
  9. Create a JPA configuration file at META-INF/persistence.xml. The persistence.xml file should contain the following content:
  10. <persistence xmlns=""
    xmlns:xsi="" xsi:schemaLocation="" version="2.0">
    	<persistence-unit name="com.liquibase.hibernate.tutorial.jpa" transaction-type="RESOURCE_LOCAL">
    			<property name="javax.persistence.schema-generation.database.action" value="none" />
    			<property name="javax.persistence.provider" value="org.hibernate.jpa.HibernatePersistenceProvider" />
    			<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
    			<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost:9090/mem:dev" />
    			<property name="javax.persistence.jdbc.user" value="dbuser" />
    			<property name="javax.persistence.jdbc.password" value="letmein" />
    			<property name="hibernate.connection.handling_mode" value="delayed_acquisition_and_release_after_transaction" />
  11. Create the folder src/main/java/com/liquibase, which will be used for entity classes. In this directory, create a file in a text editor and add the following content:
  12. package com.liquibase;
    	import javax.persistence.*;
    	public class House implements Serializable {
    		private static final long serialVersionUID = 1L;
    		private Integer id;
    		private String owner;
    		private boolean fullyPaid;
    		public Integer getId() {
    		return id;
    		public void setId(Integer id) { = id;
    		public String getOwner() {
    		return owner;
    		public void setOwner(String owner) {
    		this.owner = owner;
    		public boolean isFullyPaid() {
    		return fullyPaid;
    		public void setFullyPaid(boolean fullyPaid) {
    		this.fullyPaid = fullyPaid;
  13. Create a second file in the same directory and paste the following:
  14. package com.liquibase;
        import javax.persistence.*;
        public class Item implements Serializable {
        	private static final long serialVersionUID = 1L;
            private Integer id;
            private String name;
            private House house;
            public Integer getId() {
            	return id;
            public void setId(Integer id) {
   = id;
            public String getName() {
            return name;
            public void setName(String name) {
   = name;
    		public House getHouse() {
    		return house;
    		public void setHouse(House house) { = house;
  15. If you haven't already, first install the application using the mvnw install command, or mvnw.cmd install for Windows. The generated jar is what is referenced in the file:

  16. classpath=target\\hibernate-liquibase-0.0.1-SNAPSHOT.jar
  17. Next, generate a dbchangelog.xml file from Hibernate in the HibernateLiquibase project folder:

  18. liquibase --log-level=INFO --defaultsFile=src/main/resources/ generate-changelog
  19. Verify the project configuration by running the Liquibase status command. Open a command prompt and go to the project folder. Run the following command:
  20. liquibase --log-level=INFO --defaultsFile=src/main/resources/ status

    Example Output

    Liquibase command 'status' was executed successfully.
  21. Verify that the DATABASECHANGELOG and DATABASECHANGELOGLOCK tables were created.
  22. Using a database UI tool, you should see the new Liquibase tables.

    From a database UI tool, ensure that your database contains the table you added along with the DATABASECHANGELOG table and DATABASECHANGELOGLOCK table.


You have successfully configured your project and can begin creating changesets to migrate changes to your database using Hibernate.

Source code is available at:

Related Links