Hibernate.org Community Documentation

- Prev
- Next

## Chapter 6. Generating schema with Ant

If you'd like to generate the database schema file with the Hibernate Tools Ant task, you'll probably notice that the generated file doesn't contain definitions of audit tables. To generate also the audit tables, you simply need to use `org.hibernate.tool.ant.EnversHibernateToolTask` instead of the usual `org.hibernate.tool.ant.HibernateToolTask`. The former class extends the latter, and only adds generation of the version entities. So you can use the task just as you used to.

For example:

    <target name="schemaexport" depends="build-demo"
      description="Exports a generated schema to DB and file">
      <taskdef name="hibernatetool"
        classname="org.hibernate.tool.ant.EnversHibernateToolTask"
        classpathref="build.demo.classpath"/>

      <hibernatetool destdir=".">
        <classpath>
          <fileset refid="lib.hibernate" />
          <path location="${build.demo.dir}" />
          <path location="${build.main.dir}" />
        </classpath>
        <jpaconfiguration persistenceunit="ConsolePU" />
        <hbm2ddl
          drop="false"
          create="true"
          export="false"
          outputfilename="versioning-ddl.sql"
          delimiter=";"
          format="true"/>
      </hibernatetool>
    </target>

Will generate the following schema:

        create table Address (
            id integer generated by default as identity (start with 1),
            flatNumber integer,
            houseNumber integer,
            streetName varchar(255),
            primary key (id)
        );

        create table Address_AUD (
            id integer not null,
            REV integer not null,
            flatNumber integer,
            houseNumber integer,
            streetName varchar(255),
            REVTYPE tinyint,
            primary key (id, REV)
        );

        create table Person (
            id integer generated by default as identity (start with 1),
            name varchar(255),
            surname varchar(255),
            address_id integer,
            primary key (id)
        );

        create table Person_AUD (
            id integer not null,
            REV integer not null,
            name varchar(255),
            surname varchar(255),
            REVTYPE tinyint,
            address_id integer,
            primary key (id, REV)
        );

        create table REVINFO (
            REV integer generated by default as identity (start with 1),
            REVTSTMP bigint,
            primary key (REV)
        );

        alter table Person
            add constraint FK8E488775E4C3EA63
            foreign key (address_id)
            references Address;

Copyright © 2004 Red Hat Inc.

- PrevChapter 5. Queries
- Top of page
- Front page
- NextChapter 7. Generated tables and their content