<?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 3: Invoking the Torque Maven 1 Plugin</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 3: Invoking the Torque Maven 1 Plugin">

<p>
  With the configuration of the Torque Maven 1 Plugin completed, you can now
  generate the object model to support your database, and
  optionally create your database and all of its associated tables.
  Each of these tasks is covered in the following sections.
</p>

<p>
  Note: If you are yet to jump aboard the Maven ship you can
  download the torque-gen archive and make use of the Ant build
  file <code>build-torque.xml</code> contained therein. See the
  <a href="../generator/index.html">Generator documentation</a>
  for details.
</p>

</section>

<section name="Generating the Object Model and Associated SQL">

<p>
  The generation of your object model will produce
  Java source files that can be used to represent your
  database.  These classes enable you to create, edit,
  delete, and select objects that represent rows in
  your database tables.  In addition, Torque will
  generate SQL to create your database tables (you
  have the option of executing the SQL as demonstrated
  later in this tutorial).
</p>

<p>
  The object model consists of four classes for each
  table in your schema.  For example, the
  <em>author</em> table, defined in this tutorial,
  will result in the following four classes:
  <em>Author</em>, <em>AuthorPeer</em>,
  <em>BaseAuthor</em>, and <em>BaseAuthorPeer</em> (a
  discussion on the use of these classes is deferred
  until we write our sample application).
</p>

<p>
  To generate your object model and the associated SQL, type the
  following command in your top-level project directory:
</p>

<source><![CDATA[
maven torque
]]></source>

<p>
  A successful build will be indicated by the
  &#145;BUILD SUCCESSFUL&#146; message.
</p>

<p>
  The generated Java classes are located in the
  <em>src/java</em> directory and will be in a
  directory hierarchy matching that of the
  <code>torque.targetPackage</code> you
  specified in <em>project.properties</em>.
  These are the files that will be compiled into your
  object model classes.
</p>

<p>
  The generated SQL files are located in the
  <em>target/sql</em> directory.
  For each database schema in your
  <em>src/schema</em> directory, there will be a
  corresponding file with a <em>.sql</em> extension
  instead of <em>.xml</em> extension.  The contents of
  these files are the SQL commands that can be used to
  manually or automatically (see next section) create
  your database tables.
</p>

<p>
  To change the directory where the classes are generated,
  use the properties <code>torque.home</code>,
  <code>torque.output.dir</code>
  and/or <code>torque.java.dir</code>
  in your <code>project.properties</code> file - see
  the <a href="../generator/properties-reference.html">properties
  reference</a> for more detail).
</p>

<p>
  If you encounter errors while building, it is more
  than likely a formatting error of your database
  schema file.  Check the format of the file and make
  sure it conforms to the
  <a href="../generator/schema-reference.html">
  Torque Schema Reference</a>.
</p>

</section>

<section name="Creating the Database and Tables">

<p>
  As mentioned previously, Torque can automatically
  create your database and all of the associated
  tables for you.  However, you must first make sure
  that the appropriate database driver (the one you
  defined in <em>project.properties</em>) is in your
  classpath so that Torque can connect to your
  database and execute the generated SQL commands.
  The easiest way to accomplish this is to add the
  database driver jar to your local maven repository,
  and specify it as a dependency in your project.
  This is done as follows:
</p>

<subsection name="Adding the driver to the maven repository">

<p>
  For licensing reasons, most database drivers cannot
  be downloaded automatically by maven. Therefore,
  you have to add the driver manually to your local maven
  repository. The local maven repository
  is located by default in the directory
  <em>%HOMEDRIVE%%HOMEPATH%\.maven\repository</em>
  in windows, and <em>$HOME/.maven/repository</em>
  in linux/unix. Change into that direcory and
  create a subdirectory <em>${groupId}/jars</em>,
  where <em>${groupId}</em> is typically set to
  the name of the database (for example, use
  <em>mysql/jars</em> for mysql).
  Then, download the database driver, and copy the
  driver jar to the subdirectory you just created.
</p>

</subsection>

<subsection name="Specifying the driver dependency">

<p>
  The dependencies of a project are specified
  in a file named <em>project.xml</em> in the
  top level directory of your project.
  (This is not the only use of this file,
  see the
  <a href="http://maven.apache.org/start/ten-minute-test.html">
  maven getting started guide</a>
  and the
  <a href="http://maven.apache.org/reference/project-descriptor.html">
  Maven Project descriptor reference</a> for more
  information.)
</p>

<p>
  If you did not create a <em>project.xml</em> file yet,
  create it in the top level directory of your project
  and fill it using the following template. (If you
  already have a <em>project.xml</em>,
  just add the dependency.)
</p>

<source><![CDATA[
<project>
  <pomVersion>3</pomVersion>
  <groupId>torque</groupId>
  <id>torque-tutorial</id>
  <name>Torque</name>
  <currentVersion>3.3</currentVersion>

  <dependencies>
    <dependency>
      <artifactId>${artifactId}</artifactId>
      <groupId>${groupId}</groupId>
      <version>${version}</version>
    </dependency>
  </dependencies>
</project>
]]></source>

<p>
  Replace the variables ${artifactId},
  ${groupId} and ${version} by the values
  needed to locate the driver jar. These variables
  must be chosen such that the path
  <em>${repo}/${groupId}/${type}s/${artifactId}-${version}.${type}</em>
  points to the driver jar you copied into the
  local maven repository.
  Here, <em>${repo}</em> is the path to the
  local maven repository, ${type} is set to
  <code>jar</code> by default, and the other variables
  are set in the <em>project.xml</em>.
  If the name of the driver jar cannot be expressed as
  <em>${artifactId}-${version}.${type}</em>, rename
  the driver jar in yor local maven repository.
</p>

<p>
  For example, if you use the mysql 5.0.4 driver, 
  <code>${artifactId}</code> would be set to
  <code>mysql-connector-java</code>,
  <code>${groupId}</code> would be set to
  <code>mysql</code>,
  and <code>${version}</code> would be set to
  <code>5.0.4</code> in the <em>project.xml</em>.
</p>

</subsection>

<subsection name="Creating the database">

<p>
  <b>
    Note: Torque will <em>drop</em> the database and
    tables that it is about to create if they exist!
    You should skip this step if you are working
    with an existing database full of data.
  </b>
</p>

<p>
  To create your database, type the following command
  in the top-level directory of your project:
</p>

<source><![CDATA[
maven torque:create-db
]]></source>

<p>
  Note that creating the database might not work
  for some databases at all (e.g. oracle).
  Also, for other databases (e.g. mysql),
  the database user must be database administrator
  to be able to create the database, and often
  (e.g. mysql, postgresql), one must connect to a
  database which is different from the database
  which one wants to create (this is why there are
  different properties
  <code>torque.database.createUrl</code>
  and <code>torque.database.buildUrl</code>
  in the <em>project.properties</em>
  for creating the database and the tables,
  respectively).
</p>

<p>
  If you encounter problems in this step,
  one possible reason might be a misconfiguration
  of your <em>project.properties</em>.  Another common
  problem is that the user specified in the
  <em>project.properties</em> does not have sufficient
  privilege to create a database.  In either
  case, refer to the section above that explains the
  <em>project.properties</em> file.
  If the problems persist, skip this step 
  and create the database manually.
</p>

</subsection>

<subsection name="Creating the tables">

<p>
  To create your tables, type the following commands in
  the top-level directory of your project:
</p>

<source><![CDATA[
maven torque:id-table-init-sql
maven torque:insert-sql
]]></source>

<p>
  Note: if this tutorial had not utilized Torque's
  <em>idbroker</em> method (as described earlier), it
  would not have been necessary to execute the
  <em>id-table-init-sql</em> target.
</p>

<p>
  Success will be indicated by the &#145;BUILD
  SUCCESSFUL&#146; message.  You can also validate
  this by checking your database.  For example, the
  <em>bookstore-schema.xml</em> and
  <em>id-table-schema.xml</em>, defined in this
  tutorial, should have created a database called
  <em>bookstore</em>, with the following tables:
  <em>ID_TABLE</em>, <em>author</em>, <em>book</em>,
  and <em>publisher</em>.
</p>

<p>
  If you have difficulties in creating the tables 
  using maven, you can also execute the SQL scripts 
  generated in the directory <em>target/sql</em> 
  manually.
</p>

</subsection>

</section>

<section name="Where to next">

  <p>
    Now that you have generated all of your object model
    classes and created your database, you are ready to
    build your first Torque application.
  </p>
  <p>
    Next we will look <a href="step4.html">Configuring the Torque Runtime</a>.
  </p>

</section>

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

  </body>
</document>
