Filed Under (Hibernate, Java) by jonr on July-31-2007

I am a big fan of using Ant to do routine tasks. I just spent a bit of time figuring out how to get Hibernate 3/Ant to generate the database scripts when using annotations. The Hibernate 2 way with the SchemaExportTask doesn’t appear to work. Hopefully my notes below will save you a bit of time if you are trying to do the same.

The Hibernate 2 with hbm.xml file way:

<taskdef name=”schemaexport”
classname=”net.sf.hibernate.tool.hbm2ddl.SchemaExportTask”
classpathref=”project.class.path” />

<schemaexport
properties=”${resources.db}/database.properties”
quiet=”no”
text=”no”
drop=”yes”
delimiter=”;”
output=”${resources.db}/drop-tables.sql”>
<fileset dir=”${hibernatedoclet.dir}” includes=”**/*.hbm.xml”/>
</schemaexport>

The Hibernate 3 with Annotations way:
- Note you need to download the hibernate-tools.jar

<taskdef name=”hibernatetool”
classname=”org.hibernate.tool.ant.HibernateToolTask”
classpathref=”hibernate.export.classpath” />

<hibernatetool destdir=”${build.db.dir}”>
<annotationconfiguration configurationfile=”hibernate.cfg.xml”/>
<hbm2ddl export=”false”
create=”true”
delimiter=”;”
format=”true”
outputfilename=”create-tables.sql”/>
</hibernatetool>

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • LinkedIn

Comments:
Richard Berger on December 11th, 2009 at 11:26 pm #

Thanks much – this was a big help. It was pretty frustrating that the “Run SchemaExport” command that is built into the Hibernate Console doesn’t work for annotations.

Post a comment
Name: 
Email: 
URL: 
Comments: