Liquibase + Hibernate: Easy and Solid Database Migration
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>
(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 :-)
becomes:
Anyway, it works very well now!! :-)
Thank you
J.
Peter ___ 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 ___ 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