<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<document>
  <properties>
    <title>Torque Tutorial - Step 2: Configuring the Torque generation process</title>
    <author email="pete-apache-dev@kazmier.com">Pete Kazmier</author>
    <author email="seade@backstagetech.com.au">Scott Eade</author>
    <author email="fischer@seitenbau.de">Thomas Fischer</author>
  </properties>
  <body>

<section name="Step 2: Configuring the Torque generation process">

<p>
  The following section outlines the necessary steps to
  define your database schema and configure the Torque maven 1 plugin
  to use your schema. Upon completion, you'll be able to use
  the generator to create your object model and all of the Java
  classes that support it.  In addition, the maven 1 plugin can
  generate and execute all of the appropriate SQL commands
  to create your database, freeing you from having to do it
  manually.
</p>

<p>
  To accomplish all of the above, you need to
  create/edit the Torque maven 1 plugin properties file
  (which is providing the maven 1 plugin with the
  necessary information)
  and the Torque database schema file(s)
  (which contain the structure of your database).
  Each of these files is covered in the
  following sections.
</p>

<p>
  As a starting point, create a directory as a
  base directory for your project (also called
  the project's top level directory),
  and change into that directory.
  All the paths in the following steps will be
  relative to this base directory.
</p>

</section>

<section name="Torque Maven 1 Plugin Properties">

<p>
  The maven 1 plugin generates Java
  source/class files representing your object model,
  SQL statements for your specific database, and
  documentation.
  You configure the maven 1 plugin by setting properties in the
  <em>project.properties</em> file in root directory of your
  project.  As a starting point, use the following template
  and edit it to reflect your specific needs (
  typically you need to change the database, the database urls, 
  the database driver, the database host, the database user and password):
</p>

<source><![CDATA[
  # The name of the project Torque will generate code for.
  torque.project = bookstore

  # The target database platform.
  torque.database = mysql

  # The target package to put the generated classes in.
  torque.targetPackage = com.kazmier.om

  # The JDBC URL that Torque can use to create and
  # drop databases if instructed to do so.
  torque.database.createUrl = jdbc:mysql://127.0.0.1/mysql

  # The JDBC URL that will be used to create tables in your database.
  torque.database.buildUrl = jdbc:mysql://127.0.0.1/bookstore

  # The JDBC URL that will be used to access your database.
  torque.database.url = jdbc:mysql://127.0.0.1/bookstore

  # The JDBC database driver to use when connecting to your database.
  torque.database.driver = org.gjt.mm.mysql.Driver

  # The administrative username that has sufficient privileges to create
  # and drop databases and tables that Torque executes at generation time.
  torque.database.user = adminuser

  # The administrative password for the supplied username.
  torque.database.password = adminpassword

  # The hostname or IP address of your database server.
  torque.database.host = 127.0.0.1
]]></source>

<p>
  Setting these properties correctly is very
  important.  They enable Torque to generate all of
  the required sources and SQL for your specific
  database.  If you experience problems later in this
  tutorial, it would be wise to double-check these
  values.
</p>

<p>
  The properties that can be used in the maven 1 plugin are identical to those
  that can be used in the ant-based generator.
  For a reference as to what each property, and others, controls, please
  see the <a href="../generator/properties-reference.html">properties
  reference</a> for the Torque generator.
</p>

</section>

<section name="Torque Database Schema">

<p>
  The second file that you must edit to configure
  Torque is the database schema.  The database schema
  is an XML file that represents the structure of your
  SQL database in Torque.
  This is where you define all of your
  tables, column names and types, as well as the keys
  used to index these tables.
</p>

<p>
  Your database schema file should be located in the
  <em>src/schema</em> directory under the base of your project.
  In this directory, you will
  create two XML files: <em>id-table-schema.xml</em> and
  <em>project-schema.xml</em>.  The
  <em>id-table-schema.xml</em> file is used internally
  by Torque's IDBroker service (which is a database
  independent method for generating unique IDs).
  <em>project-schema.xml</em> is where you'll define
  your database schema. The name of
  your database schema file must end with
  &#145;-schema.xml&#146; because Torque will only
  generate object models for files ending with that
  pattern.
</p>

<p>
  In this tutorial, we will use a simple
  database that might be used to support a bookstore
  application.  The database will contain three
  tables: author, publisher, and book.  The first
  table will contain author information (first
  and last name).  The second table will
  contain publisher information (name).  And the third
  table will contain book information (title, and
  ISBN).  The author id and publisher id will be
  foreign keys in the book table.  The schema
  representation for this database is stored
  in the file <em>project-schema.xml</em>, which should
  be created in the <em>src/schema</em> directory
  and contain the following:
</p>

<source><![CDATA[
<!DOCTYPE database SYSTEM
 "http://db.apache.org/torque/dtd/database_3_3.dtd">

<database
  name="bookstore"
  defaultIdMethod="idbroker">

  <table name="publisher" description="Publisher Table">
    <column
      name="publisher_id"
      required="true"
      primaryKey="true"
      type="INTEGER"
      description="Publisher Id"/>
    <column
      name="name"
      required="true"
      type="VARCHAR"
      size="128"
      description="Publisher Name"/>
  </table>
  <table name="author" description="Author Table">
    <column
      name="author_id"
      required="true"
      primaryKey="true"
      type="INTEGER"
      description="Author Id"/>
    <column
      name="first_name"
      required="true"
      type="VARCHAR"
      size="128"
      description="First Name"/>
    <column
      name="last_name"
      required="true"
      type="VARCHAR"
      size="128"
      description="Last Name"/>
  </table>
  <table name="book" description="Book Table">
    <column
      name="book_id"
      required="true"
      primaryKey="true"
      type="INTEGER"
      description="Book Id"/>
    <column
      name="title"
      required="true"
      type="VARCHAR"
      size="255"
      description="Book Title"/>
    <column
      name="isbn"
      required="true"
      type="VARCHAR"
      size="24"
      javaName="ISBN"
      description="ISBN Number"/>
    <column
      name="publisher_id"
      required="true"
      type="INTEGER"
      description="Foreign Key Publisher"/>
    <column
      name="author_id"
      required="true"
      type="INTEGER"
      description="Foreign Key Author"/>
    <foreign-key foreignTable="publisher">
      <reference
        local="publisher_id"
        foreign="publisher_id"/>
    </foreign-key>
    <foreign-key foreignTable="author">
      <reference
        local="author_id"
        foreign="author_id"/>
    </foreign-key>
  </table>
</database>
]]></source>

<p>
  There are several items of importance to note.  The
  <em>database</em> element's <em>name</em> attribute
  must be the same as the database name specified by
  the <em>databaseUrl</em> property in
  <em>project.properties</em>; likewise, the run-time
  properties (described in step 4) should
  also reflect this value.  Failure to do so will
  prevent Torque from creating your database tables
  (if instructed to do so) or prevent your object
  model from working properly.
</p>

<p>
  Another item of importance is the <em>database</em>
  element's <em>defaultIdMethod</em> attribute.  This
  attribute specifies the default method that Torque
  will use to generate IDs for primary keys (columns
  with the <em>primaryKey</em> attribute set to
  <em>true</em>: <em>book_id</em>,
  <em>publisher_id</em>, and <em>author_id</em>) in
  your database tables.  There are several possible
  values:
</p>

<table>
  <tr>
    <th>Property</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>idbroker</td>
    <td>
      Instructs Torque to use its
      <a href="../runtime/reference/relevant-classes.html#ID_Broker">IDBroker</a>
      service to generate IDs in a database agnostic manner.
      This is the method that will be used in this tutorial.
    </td>
  </tr>
  <tr>
    <td>native</td>
    <td>
      Instructs Torque to use the underlying
      database's mechanism to generate IDs (varies
      per database).
    </td>
  </tr>
  <tr>
    <td>none</td>
    <td>
      Instructs Torque to not generate IDs.  This
      can be useful in some situations.
    </td>
  </tr>
</table>

<p>
  The <em>defaultIdMethod</em> selected will be used
  for all tables in your schema unless an individual
  <em>table</em> element contains the
  <em>idMethod</em> attribute, in which case, its
  value will override the <em>defaultIdMethod</em>.
  <em>idMethod</em> takes the same values as
  <em>defaultIdMethod</em>.
</p>

<p>
  Another common mistake is to forget that XML is
  <b>case-sensitive</b>.  All of the elements and
  attributes must be specified according to the
  <a href="../generator/database.dtd.txt">DTD</a>
  for the database schema.  In addition, you must
  include the XML declaration and DTD specification in
  your database schema file.  Failure to do so can
  result in errors.
</p>

<p>
  To initialize the IDBroker service, create a file
  called <em>id-table-schema.xml</em> in the
  <em>src/schema</em> subdirectory of your project's
  base directory. It should have the following contents:
</p>

<source><![CDATA[
<!DOCTYPE database SYSTEM
 "http://db.apache.org/torque/dtd/database_3_2.dtd">

<database name="bookstore">
  <table name="ID_TABLE" idMethod="idbroker">
    <column
      name="ID_TABLE_ID"
      required="true"
      primaryKey="true"
      type="INTEGER"/>
    <column
      name="TABLE_NAME"
      required="true"
      size="255"
      type="VARCHAR"/>
    <column
      name="NEXT_ID"
      type="INTEGER"/>
    <column
      name="QUANTITY"
      type="INTEGER"/>
    <unique>
      <unique-column name="TABLE_NAME"/>
    </unique>
  </table>
</database>
]]></source>

<p>
  Note that again, the <em>name</em> attribute to the
  <em>database</em> element has the same value as in the
  <em>project-schema.xml</em>.
</p>

<p>
  For additional information on
  the XML elements and attributes, please refer to the
  <a href="../generator/schema-reference.html">
  Torque Schema Reference</a>.
</p>

</section>


<section name="Where to next">

  <p>
    That completes the configuration of the Torque generator.
    You are now ready to start building your object model
    and creating your database.
  </p>
  <p>
    Next we will look at
    <a href="step3.html">Invoking the Torque Maven 1 plugin</a>.
  </p>

</section>

<section name="User Comments">
  <a href="http://wiki.apache.org/db-torque/TutorialUserCommentsStep2">User comments</a>
  for this step
</section>

  </body>
</document>
