Torque Database Schema Reference

Torque was developed as part of the Turbine Framework (version 2.1). It is now decoupled and can be used by itself. Starting with Turbine 2.2 the coupled version of Torque is deprecated. You should upgrade to use the decoupled version.

The Torque Database Schema Reference attempts to explain what the different elements and attributes are when defining your own database schema. In addition I will attempt to explain what attributes mean what in the different databases currently supported.

Elements and their attributes

Some of the following examples are taken from the project-schema.xml document in the src/conf/torque/schema.

Element: database

The database element and its relevant attributes.

    <database name="MY_DATABASE"
            defaultIdMethod="idbroker"
            package="com.myapp.om"
            baseClass="com.myapp.om.BaseClass"
            basePeer="com.myapp.om.BasePeer"
            defaultJavaNamingMethod="underscore">
      <table name="SIMPLE">
        <!-- table info goes here -->
      </table>
    </database>
    

The database element has 6 attributes associated with it, they are:

  • name - The name of the database being referenced
  • defaultIdMethod - How will the primary keys be created, defaults to "none"
  • defaultJavaNamingMethod - indicates how a schema table or column name is mapped to a java class or method name respectively
  • package - used for OM Peer generation
  • baseClass - used for OM generation
  • basePeer - used for OM Peer generation
The last four will be explained in detail below.

The database element can contain the following elements:

  • table - one or more

Attribute: defaultIdMethod

By defining this attribute at the database level it applies the defaultIdMethod to those tables which do not have an idMethod attribute defined. The attribute defaultIdMethod has 5 possible values, they are:

  • idbroker - This allows turbine to generate the IDs through its IDBroker Service
  • native - Turbine will determine how the database will auto-generate IDs
  • autoincrement - deprecated, please use native
  • sequence - deprecated, please use native
  • none - Typically used if you do not want IDs generated

Attribute: defaultJavaNamingMethod

This attribute determines how table or column names, from the name attribute of the table or column element, are converted to a Java class or method name respectively when creating the OM java objects. defaultJavaNamingMethod can contain 3 different values:

  • nochange - Indicates no change is performed.
  • underscore - Underscores are removed, First letter is capitalized, first letter after an underscore is capitalized, the rest of the letters are converted to lowercase.
  • javaname - Same as underscore, but no letters are converted to lowercase.

Attribute: package

The base package in which this database will generate the Object Models associated with it. This overrides the targetPackage property in the torque build.properties file.

Attribute: baseClass

The base class to use when generating the Object Model. This class does not have to extend org.apache.turbine.om.BaseObject.

Attribute: basePeer

The base peer to use when generating the Object Model Peers. Unlike baseClass, basePeer should extend BasePeer at some point in the chain, ie - it needs to be the superclass.

Element: table

The table element and its relevant attributes

    <table name="MY_TABLE"
            javaName="table"
            idMethod="idbroker"
            skipSql="false"
            baseClass="com.myapp.om.table.BaseClass"
            basePeer="com.myapp.om.table.BasePeer"
            javaNamingMethod="underscore">
        <!-- column information here -->
    </table>
    

The table element has 9 attributes associated with it, they are:

  • name - The name of the database being referenced
  • javaName - How this table will be referenced in java
  • idMethod - How will the primary keys be created, defaults to "none"
  • skipSql - Whether or not to skip SQL generation for this reference
  • abstract - Whether or not to generate the class as Abstract or not
  • alias - The table alias
  • baseClass - used for OM Peer generation
  • basePeer - used for OM Peer generation
  • javaNamingMethod - Specifies how the name attribute is converted to the Java class name of the coresponding OM object. this attribute overides the defaultJavaNamingMethod attribute of the database element

The table element can contain the following elements:

  • column - one or more
  • foreign-key - 0 or more
  • index - 0 or more
  • unique - 0 or more
  • id-method-parameter - 0 or more

Attribute: javaName

This is the java class name to use when generating the Table or column. If this is missing the java name is generated in the following manner:
Underscores are removed, first letter and first letter after each underscore is uppercased, all other letters are lowercased. So YOUR_TABLE_NAME would become YourTableName.

Element: column

The column element and its relevant attributes

    <column name="MY_COLUMN"
            javaName="Column"
            primaryKey="true"
            required="true"
            size="4"
            type="VARCHAR"
            javaNamingMethod="underscore">
        <!-- inheritance info if necessary -->
    </column>
            
    

The column element has 9 attributes associated with it, they are:

  • name - The name of the column being referenced
  • javaName - How this column will be referred to in Java
  • primaryKey - Is this a primary key or not (true or false)
  • required - Whether a value is required in this field (true or false)
  • type - What type of column is it? (Covered below), defaults to VARCHAR
  • size - How many characters or digits can be stored?
  • default - Default value to insert into field if it is missing.
  • autoIncrement - Whether or not to auto-increment this field, defaults to "false"
  • inheritance - ?
  • inputValidator - ?

The column element can contain the following elements:

  • inheritance - 0 or more

Element: inheritance

The inheritance element and its relevant attributes

    <inheritance key="key"
            class="classname"
            extends="mybase"/>
            
    

The inheritance element has 3 attributes associated with it, they are:

  • key - ?
  • class - ?
  • extends - ?

Element: foreign-key

The foreign-key element and its relevant attributes

    <foreign-key foreignTable="MY_TABLE">
    <!-- reference info -->
    </foreign-key>
            
    

The foreign-key element has 1 attribute associated with it, it is:

  • foreignTable - the name of the table being referenced

The foreign-key element can contain the following elements:

  • reference - 1 or more

Element: reference

The reference element and its relevant attributes

    <reference local="FK_TABLE_ID" foreign="PK_COLUMN_ID"/>
            
    

The reference element has 2 attributes associated with it, they are:

  • local - the local reference
  • foreign - the foreign key reference