SQL Zone is brought to you in partnership with:

Hacking on GraphHopper - a Java road routing engine. Peter has posted 62 posts at DZone. You can read more from them at their website. View Full User Profile

Liquibase + Hibernate: Easy and Solid Database Migration

06.21.2010
| 11912 views |
  • submit to reddit

As I pointed out earlier liquibase is a stable and nice migration tool for SQL databases in the Java land. The problem I had is that I couldn’t get it working with hibernate while using annotations.

Now just for my personal memory here are the steps to get it working. Download liquibase 1.9.5 (couldn’t get it working with 2.0.0 :-( ) and put the following libs in the liquibase/lib folder:

dom4j-1.6.1.jar
h2-1.2.137.jar #or your prefered jdbc database driver
hibernate-annotations-3.5.1-Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
hibernate-core-3.5.1-Final.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
slf4j-api-1.6.0.jar

You will need to put the 4 hibernate jars into the classpath parameter, too. To do a diff and update the changelog file via the command line do the following:

liquibase --logLevel=FINE \
--driver=org.h2.Driver \
--classpath=$CP \
--url=jdbc:h2:~/.myapp/h2db \
--username=sa \
--password=tmp \
--changeLogFile=src/main/resources/dbchangelog.xml \
diffChangeLog \
--baseUrl=hibernate:src/main/resources/hibernate.cfg.xml

This means you compare the ‘old’ database with the new hibernate config. If you have problems while set-up you can look directly into the source file.

BTW: here is the pom snippet for the hibernate deps:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.1-Final</version>
</dependency>

From http://karussell.wordpress.com/2010/06/20/liquibase-hibernate-annotations-easy-and-solid-database-migration/

Published at DZone with permission of its author, Peter Karussell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jan Van Bulck replied on Mon, 2010/06/21 - 4:24pm

Hi Peter,

This looks very interesting.  

I've got it running, but the result is not exactly what I was hoping for.... Maybe I was expecting the wrong thing...

The result I observe:

a set of changelog entries, that describe:
the migration of my current (new) hibernate mapping-definitions to the actual (old) image of the database.

What I was expecting: 

a set of changelog entries, that describe:
the migration from my actual (old) database objects to my (new) set of hibernate mapping-definitions

Can you give me a clue?  Am I doing something wrong?  Or am I using the wrong tool?

 Thank you very much!!

J.

Jan Van Bulck replied on Mon, 2010/06/21 - 5:12pm

The answer was too obvious...

 It was just a matter of flipping the base- and target system :-)

 --username=un \
 --password=pw \
 --url=jdbc:mysql:///mydb \
 diffChangeLog \
 --baseUrl="hibernate:/hibernate.hbm.xml"

 becomes:

 --url="hibernate:/hibernate.hbm.xml" \
 diffChangeLog \
 --baseUrl=jdbc:mysql:///mydb \
 --baseUsername=un \
 --basePassword=pw

Anyway, it works very well now!! :-)

 Thank you

J.

Peter Karussell replied on Tue, 2010/06/22 - 3:23am

Uh, ok. Strange.

I was experimenting with the initial changelog.xml and with the documented command I obtained the result what I expected (hibernate as new; db as old schema)

So, thanks for sharing! I think that would be my question when I want to update the schema the next time :-)

Peter Karussell replied on Tue, 2010/07/13 - 5:37am in response to: Jan Van Bulck

Now I need you help (incremental updates does not work for me, only the initial changelog.xml): did you use annotations too?

See also the discussion here

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.