Saturday, May 17, 2008

Maven2 hibernate DDL generation

I needed a way to get hibernate to generate DDL for me and I ended up using the hibernate maven plugin. It took a lot of trial and error to get it working so hopefully this will save someone some time when they try to do the same thing.

In case you are not familiar, Hibernate is an ORM package for Java that allows you to treat your database like a collection of objects (sort of). Maven2 is a java build system (like ant or make). DDL is data definition language and tends to be unique for every database so if you want a project to work with multiple databases then you have to create DDL for each one. That's where this comes in to play. Normally hibernate will generate DDL and run it for you but this is not always desirable. Especially when dealing with production systems which already have a large database. This will allow you to generate the DDL right in the source of your project and then simply commit the DDL files back to your source repository. If you change your hibernate mapping files then regenerate the DDL and commit the changes.

I created a simple folder with the right pom.xml and the other needed support files to allow a developer to generate DDL by just running mvn install in their ddl folder (the one with these files). You can download that to get started. There is a little bit of setup required which is detailed on my wiki page about generating DDL using hibernate and maven2. Checkout the readme which explains how to use it. I would think it is probably 10 minutes of effort to add this to an existing hibernate project which is using maven 2.

1 comment:

james said...

THis is just so helpful tutorial!! Thanks!