Search code examples
javahibernateantormhbm2ddl

Schema export with hibernate annotations


I'm using hibernate annotations and i want to export my database schema.

Similar to the schemaexporttask with hbm xml files.


Solution

  • Indeed, the original Hibernate Core SchemaExportTask can only handle Hibernate XML mapping files, not annotations. What you need is the HibernateToolTask that comes with Hibernate Tools.

    Here is an Usage example adapted from Java Persistence With Hibernate:

    <taskdef name="hibernatetool"
             classname="org.hibernate.tool.ant.HibernateToolTask"
             classpathref="project.classpath"/>
      <target name="schemaexport" depends="compile, copymetafiles"
              description="Exports a generated schema to DB and file">
        <hibernatetool destdir="${basedir}">
          <classpath path="${build.dir}"/>
          <configuration 
              configurationfile="${build.dir}/hibernate.cfg.xml"/>
          <hbm2ddl
              drop="true"
              create="true"
              export="true"
              outputfilename="helloworld-ddl.sql"
              delimiter=";"
              format="true"/>
        </hibernatetool>
    </target>
    

    See also