Hibernate 3 Annotations & Ant
jonr originally posted this on Jon Rose's Blog.
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>