Skip to content
Nathan Voxland edited this page Oct 10, 2013 · 12 revisions

Liquibase Hibernate Extension

This Liquibase extension lets you use your Hibernate configuration as a comparison database for diff, diffChangeLog and generateChangeLog.

Normal workflow using this extension is:

  1. Edit your Hibernate mapped classes as needed (add and remove classes and attributes)
  2. Run liquibase --changeLogFile=YOUR_FILE.xml --databaseUrl=hibernate:yourconfig.xml --comparisonUrl=jdbc:yourdatabase
  3. Check that the modified YOUR_FILE.xml does what you expect, edit it if it does not
  4. Run liquibase --changeLogFile=YOUR_FILE.xml --databaseUrl=jdbc:yourdatabase diffChangeLog
  5. Repeat

If you specify an existing changeLogFile, each run of diffChangeLog will append to the file.

Why not use HBM2DDL?

  1. The Hibernate documentation says not to use it for production
  2. Stack Overflow says not to use it

The main problem with HBM2DDL is that it automatically applies changes that it thinks is right. For many cases that can be fine, but as with any diff-based database tool it can easily destroy data because it matters HOW the database gets from state A to state B. For example, if you rename your Person class to Employee, hbm2ddl will just create a new table called EMPLOYEE rather than perform the rename you want.

What makes Liquibase different is that it does not figure out changes based on schema comparisons but instead runs the changes you said need to be made. The Liquibase Hibernate extension makes creating those changes easier by automatically creating a rough draft of those changes. Most of the time the changes can be run directly, but in the cases where it guesses wrong you have the opportunity to fix the proposed changes before they are applied to your database and all other databases down the pipeline.

Usage

Once installed, the liquibase-hibernate integration allows you to treat your hibernate configuration and classes like any other database by using a special connection url syntax.

Three styles of connection URLs are supported:

hibernate:classic

The hibernate classic URL allows you to either reference your hibernate config XML file or specify a class name that implements liquibase/ext/hibernate/customfactory/CustomClassicConfigurationFactory

Database URL Examples:

hibernate:classic:com/example/hibernate.cfg.xml
hibernate:classic:hibernate.cfg.xml
hibernate:classic:com.example.MyConfigFactory
hibernate:classic:com/example/hibernate.cfg.xml?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

hibernate:ejb3

The hibernate ejb3 URL allows you to either reference a persistence-unit defined in your persistence.xml or specify a class name that implements liquibase/ext/hibernate/customfactory/CustomEjb3ConfigurationFactory

Database URL Examples:

hibernate:ejb3:myPersistenceUnit
hibernate:ejb3:com.example.MyConfigFactory
hibernate:ejb3:myPersistenceUnit?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

hibernate:spring

The hibernate spring URL allows you to either reference your a bean within a spring config or specify a comma separated list of package containing hibernate or javax.persistence annotated classes to use. Spring is a required dependency, even if you are listing a package and not a spring class.

  • If you are defining a spring config file, bean is a required URL parameter.
  • If you are defining a package to scan, dialect is a required URL parameter.

Database URL Examples:

hibernate:spring:com/example/spring.xml?bean=sessionFactory
hibernate:spring:com.example?dialect=org.hibernate.dialect.MySQL5Dialect
hibernate:spring:com.example.employee,com.example.auction?dialect=org.hibernate.dialect.MySQL5Dialect

URL Parameters

For all url styles, you can specify hibernate parameters using URL-style format such:

hibernate:classic:com/example/hibernate.cfg.xml?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

Any properties defined in the URL will override parameters set in the standard configuration file(s).

Installation

Simply download the liquibase-hibernate jar from the release page and add it to your classpath. You can also add a dependency through Maven (group: org.liquibase.ext, artifact liquibase-hibernate3 or liquibase-hibernate4)

Compatiblity

The Liquibase-Hibernate extension requires Liquibase 3.0.6+ and Java 1.6+. Use liquibase-hibernate3.jar or liquibase-hibernate4.jar depending on your hibernate version.

Project Interaction

Use the issue tracker to log any issues or feature requests

Pull requests are greatly appreciated

Clone this wiki locally