Introduction

The Torque templates are a set of templates for the Torque generator to produce code and SQL for a given data model. The data model needs to follow the torque schema.
In the templates, the different generation goals (om classes, sql, doc...) are organized in different packages. For each generation goal, the correct package needs to be accessed. This is described in detail in the following sections.
In the following, it is assumed that the Torque Maven plugin is used to process the templates. For using the Torque Ant tasks, modify the instructions according to the Torque Ant tasks documentation,

Preparation

For adding the Torque Maven plugin to your build, add the following to your pom.xml:

    <plugin>
      <groupId>org.apache.torque</groupId>
      <artifactId>torque-maven-plugin</artifactId>
      <version>4.0-beta1</version>
      <executions>
         ...(see below)
      </executions
      <dependencies>
        <dependency>
          <groupId>org.apache.torque</groupId>
          <artifactId>torque-templates</artifactId>
          <version>4.0-beta1</version>
        </dependency>
      </dependencies>
    </plugin>
    

Generation of om classes

For generating the OM classes, add the following execution to the executions list of the torque-generator plugin:

        <execution>
          <id>generate-sources</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <packaging>classpath</packaging>
            <configPackage>org.apache.torque.templates.om</configPackage>
            <sourceDir>${torque.schema.source.dir}</sourceDir>
            <options>
              <torque.om.package>${torque.target.package}</torque.om.package>
            </options>
          </configuration>
        </execution>
    

Replace ${torque.schema.source.dir} with the directory where you put your database schemata (e.g. /src/main/schema). Replace ${torque.target.package} with the base package for your generation, e.g. org.apache.torque.test.

You can add additional options to the configuration inside the options tag. The available options are described below.

This execution will generate the om code for all source files in the schema directory ending on -schema.xml, but excluding id-table-schema.xml. The output will be produced in the directories target/generated-sources and src/main/generated-java.

See the Customizing page for a documentation of available options which can be used to customize the generated output.

Generation of ddl sql

For generating the data description language(ddl) sql for the tables, add the following execution to the executions list of the Torque Maven plugin:

        <execution>
          <id>generate-sql</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <packaging>classpath</packaging>
            <configPackage>org.apache.torque.templates.sql</configPackage>
            <sourceDir>${torque.schema.source.dir}</sourceDir>
            <newFileTargetDir>target/generated-sql</newFileTargetDir>
            <compileNewFileTargetDir>false</compileNewFileTargetDir>
            <compileModifiedFileTargetDir>false</compileModifiedFileTargetDir>
            <options>
              <torque.database>${torque.target.database}</torque.database>
            </options>
          </configuration>
        </execution>
    

Replace ${torque.target.database} with the target database type (e.g. mysql, oracle). Replace ${torque.schema.source.dir} with the directory where you put your database schemata (e.g. /src/main/schema).

This will generate the sql code for all source files in the schema directory ending on -schema.xml; the output goes to the directory target/generated-sql.

Generation of html documentation

For generating html documentation for the tables, add the following execution to the executions list of the Torque Maven plugin:

        <execution>
          <id>generate-html-doc</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <packaging>classpath</packaging>
            <configPackage>org.apache.torque.templates.doc.html</configPackage>
            <sourceDir>${torque.schema.source.dir}</sourceDir>
            <newFileTargetDir>target/generated-docs</newFileTargetDir>
            <compileNewFileTargetDir>false</compileNewFileTargetDir>
            <compileModifiedFileTargetDir>false</compileModifiedFileTargetDir>
            <options>
              <torque.om.package>${torque.target.package}</torque.om.package>
              <torque.database>${torque.target.database}</torque.database>
            </options>
          </configuration>
        </execution>
    

Replace ${torque.target.database} with the target database type (e.g. mysql, oracle). Replace ${torque.schema.source.dir} with the directory where you put your database schemata (e.g. /src/main/schema). Replace ${torque.target.package} with the base package for your generation, e.g. org.apache.torque.test.

This will generate the html documentation for all source files in the schema directory ending on -schema.xml. The output will be produced in the directory target/generated-docs.

Generation of xdoc documentation

For generating xdoc documentation for the tables (to be included into your maven site), add the following execution to the executions list of the Torque Maven plugin:

        <execution>
          <id>generate-xdoc</id>
          <phase>pre-site</phase>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <packaging>classpath</packaging>
            <configPackage>org.apache.torque.templates.doc.xdoc</configPackage>
            <sourceDir>${torque.schema.source.dir}</sourceDir>
            <newFileTargetDir>target/generated-xdocs</newFileTargetDir>
            <compileNewFileTargetDir>false</compileNewFileTargetDir>
            <options>
              <torque.om.package>${torque.target.package}</torque.om.package>
              <torque.database>${torque.target.database}</torque.database>
            </options>
          </configuration>
        </execution>
    

Replace ${torque.target.database} with the target database type (e.g. mysql, oracle). Replace ${torque.schema.source.dir} with the directory where you put your database schemata (e.g. /src/main/schema). Replace ${torque.target.package} with the base package for your generation, e.g. org.apache.torque.test.

This will generate the xdoc documentation for all source files in the schema directory ending on -schema.xml. The output is produced in the directory target/generated-xdocs. You may then want to add the following configuration to your pom to include this directory in your site:

    <plugin>
      <artifactId>maven-site-plugin</artifactId>
      <groupId>org.apache.maven.plugins</groupId>
      <configuration>
        <xdocDirectory>target/generated-xdocs</xdocDirectory>
      </configuration>
    </plugin>
    

Generation of XML schema from an existing database

For extracting the structure of an existing database into a schema file, add the following execution to the executions list of the Torque Maven plugin:

        <execution>
          <id>generate-schema-from-jdbc</id>
          <phase>generate-test-sources</phase>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <packaging>classpath</packaging>
            <configPackage>org.apache.torque.templates.jdbc2schema</configPackage>
            <newFileTargetDir>target/generated-schema</newFileTargetDir>
            <newFileTargetDirUsage>none</newFileTargetDirUsage>
            <options>
              <torque.jdbc2schema.driver>${torque.driver}</torque.jdbc2schema.driver>
              <torque.jdbc2schema.url>${torque.database.url}</torque.jdbc2schema.url>
              <torque.jdbc2schema.user>${torque.database.user}</torque.jdbc2schema.user>
              <torque.jdbc2schema.password>${torque.database.password}</torque.jdbc2schema.password>
              <torque.jdbc2schema.schema>${torque.database.schema}</torque.jdbc2schema.schema>
            </options>
          </configuration>
        </execution>
    

Replace ${torque.driver} with the fully qualified class name of the database driver. Replace ${torque.database.url} with the url to connect to the database. Replace ${torque.database.user} and ${torque.database.password} with the database username and the corresponding password. You may want to replace ${torque.database.schema} with the database schema to read, if not please remove the line containing it.