Sharing the Code

Programming stuff that might be useful to others

hbm2ddl Ant Task with Hibernate 4.1

Grails has a nice feature where it will create the database schema from your Java model classes. This is actually a feature of Hibernate which Grails is built on. In Hibernate you can turn on the option in your hibernate config file (hibernate.cfg.xml) as in:

[sourcecode language=”xml”] update [/sourcecode]

See Hibernate Reference Manual, scroll down to table 3.7

I prefer to put this in an Ant task using Hibernate Tools so I have control over when the database schama is changed. Hibernate Tools is part of JBoss Tools for Eclipse which you can get here. Stable versions are only available for Eclipse 3.6 or less. For Eclipse 3.7 you will need to use a development milestone. The hibernatetool ant task currently works with Hibernate 3 so there is a little bit of hacking to get it to work with Hibernate 4. I found it requires these JARs:

from eclipse\plugins\org.hibernate.eclipse.libs_3.4.0...\lib\tools:
  freemarker.jar
  hibernate-tools.jar

from eclipse\plugins\org.hibernate.eclipse.libs_3.4.0...\lib\hibernate
  slf4j-api-1.5.8.jar
  slf4j-log4j12-1.5.8.jar

from hibernate 4.1 download\lib\required:
  jboss-logging-3.1.0.GA.jar
  jboss-transaction-api_1.1_spec-1.0.0.Final.jar

It also requires some classes from Hibernate 3 so you will need to extract some classes from the hibernate 3 jar which you can find in eclipse\plugins\org.hibernate.eclipse.libs_3.4.0…\lib\hibernate. Rename hibernate3.jar to hibernate3.zip and extract all classes in org\hibernate\engine\query\sql and org\hibernate\util. Then zip these extracted class files into a zip file and change the extension to jar. I called mine hibernate3-partial.jar. In my project I copied all these jars to lib\compiletime\ant\hibernate.

Below is Ant’s build.xml. First I compile my model clsses with a particular classpath and then I generate the database schema giving a different classpath to hibernate tool because it requires some Hibernate 3 classes and some extra jars.

[sourcecode language=”xml”]






























[/sourcecode]

If you were not using annotations for Hibernate configuration you would use <configuration> instead of <annotationconfiguration>. I’ve also specified a naming strategy so my classes and properties get converted to underscore case e.g. MethodStep class would map to method_step table:

[sourcecode language=”java”]
import org.hibernate.cfg.ImprovedNamingStrategy;

public class ASNamingStrategy extends ImprovedNamingStrategy {

public String classToTableName(String className) {
return UtilMethods.changeCase(UtilMethods.unqualify(className), Case.CAMEL_CASE, Case.UNDERSCORE_CASE);
}

public String propertyToColumnName(String propertyName) {
return UtilMethods.changeCase(propertyName, Case.CAMEL_CASE, Case.UNDERSCORE_CASE);
}

public String propertyToTableName(String className, String propertyName) {
return classToTableName(className)+”_”+propertyToColumnName(propertyName);
}
}
[/sourcecode]

This is the layout of my lib directory referrenced in the ant script above:

lib
  compiletime
    ant
      hibernate
        freemarker.jar
        hibernate3-partial.jar
        hibernate-tools.jar
        jboss-logging-3.1.0.GA.jar
        jboss-transaction-api_1.1_spec-1.0.0.Final.jar
        slf4j-api-1.5.8.jar
        slf4j-log4j12-1.5.8.jar
  runtime
    hibernate
      antlr-2.7.7.jar
      c3p0-0.9.1.jar
      dom4j-1.6.1.jar
      hibernate-c3p0-4.1.1.Final.jar
      hibernate-commons-annotations-4.0.1.Final.jar
      hibernate-core-4.1.1.Final.jar
      hibernate-jpa-2.0-api-1.0.1.Final.jar
      javassist-3.15.0-GA.jar
    logging
      commons-logging-1.0.4.jar
      log4j-1.2.16.jar
    mysql-connector-java-5.1.18-bin.jar

Comments are closed