<?xml version="1.0" encoding="UTF-8"?>
<!--
  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.
-->
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
<document> 
  <header> 
    <title>Running the Torque Tutorial on Derby</title> 
  </header> 
  <body> 

<section>
  <title>Overview</title>
<p>
<a href="http://db.apache.org/torque">Torque</a>, an Apache DB subproject,
is a persistence layer that performs object mapping between java 
objects and a relational database. 
</p>
<p>
Torque 3.2 has an
<a href="http://db.apache.org/torque/releases/torque-3.2/releases/torque-3.2/tutorial/index.html">excellent 
tutorial</a> that uses MySQL.
This page summarizes the changes 
required for each step to run the tutorial on Derby 10.1
using the Derby embedded JDBC driver. 
The instructions below assume Derby 10.1.3.1 -- modify those instructions
to match the specific release you installed.
(These instructions have also been verified to work with the Derby 10.2 beta.)
</p>

<p>
Currently Torque just works with the embedded jdbc driver;
it doesn't work with the
Derby Network Client JDBC driver or the
IBM DB2 Universal JDBC driver because of an issue with the Village software
it uses.
Details are in
<a href="http://issues.apache.org/jira/browse/DERBY-142">DERBY-142</a>.
</p>

</section>

<section>
  <title>Step 1</title>
</section>

<p>
No changes to this step are required.
</p>

<section>
  <title>Step 2</title>

  <section>
    <title>Torque Generator Properties</title>

    <p>Below is a <code>project.properties</code> file that works
      with the Derby embedded driver:</p>

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

   # The target database platform.
   torque.database = derby

   # 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.
   # Derby note: Manually create/drop databases
   torque.database.createUrl =

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

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

   # The JDBC database driver to use when connecting to your database.
   torque.database.driver = org.apache.derby.jdbc.EmbeddedDriver

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

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

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

  </section>

</section>


<section>
  <title>Step 3</title>

  <section>
     <title>Adding the driver to the maven repository</title>

<p>
I added the Derby jar to my local maven repository like this:
</p>

<source>
  mkdir ~/.maven/repository/derby
  mkdir ~/.maven/repository/derby/jars
  cp /opt/Apache/db-derby-10.1.3.1-bin/lib/derby.jar ~/.maven/repository/derby/jars/derby-10.1.3.1-bin.jar
</source>

<p>
There are probably other ways for incorporating the Derby jar
that don't require renaming the jar to include the version.
</p>


  </section>

  <section>
     <title>Specifying the driver dependency</title>
<p>
Here is the complete <code>project.xml</code> file:
</p>

<source>
   &lt;project&gt;
     &lt;pomVersion&gt;3&lt;/pomVersion&gt;
     &lt;groupId&gt;torque&lt;/groupId&gt;
     &lt;id&gt;torque-tutorial&lt;/id&gt;
     &lt;name&gt;Torque&lt;/name&gt;
     &lt;currentVersion&gt;3.2&lt;/currentVersion&gt;

     &lt;dependencies&gt;

       &lt;dependency&gt;
         &lt;artifactId&gt;derby&lt;/artifactId&gt;
         &lt;groupId&gt;derby&lt;/groupId&gt;
         &lt;version&gt;10.1.3.1-bin&lt;/version&gt;
       &lt;/dependency&gt;

     &lt;/dependencies&gt;
   &lt;/project&gt;
</source>

  </section>

  <section>
     <title>Creating the database</title>

<p>
The Torque Tutorial instructions mention that 
<code>maven torque:create-db</code>
doesn't work for
all databases, and I didn't find a way to make it work for Derby.
(I also didn't try very hard, so it might be possible.)
</p>

<p>
Manually create the database in the root directory of
your Torque project like this:
</p>

<source>
   java org.apache.derby.tools.ij
   ij version 10.1
   ij&gt; connect 'jdbc:derby:bookstore;create=true';
   ij&gt; quit; 
</source>

<p>
Now set the
<code>DERBY_SYSTEM_HOME</code> environment variable to this directory location.
You'll use it in Step 6
to set the <code>derby.system.home</code> property 
so Derby will know where to find the database you created.
For example, here is how I set it on my Linux machine:
</p>

<source>
   export DERBY_SYSTEM_HOME=/home/jta/Apache/TorqueTutorial
</source>

<p>
If I were on Windows, I would have set the variable like this instead:
</p>

<source>
   set DERBY_SYSTEM_HOME=C:\home\jta\Apache\TorqueTutorial
</source>

  </section>

  <section>
     <title>Creating the tables</title>
<p>
No changes to this step are required.
</p>
  </section>
</section>

<section>
  <title>Step 4</title>

  <section>
     <title>Torque Run-Time Properties</title>
<p>
Here is the complete <code>src/conf/torque.properties</code> file:
</p>

<source>
   torque.database.default = bookstore
   torque.database.bookstore.adapter = derby

   #Using commons-dbcp
   torque.dsfactory.bookstore.factory = org.apache.torque.dsfactory.SharedPoolDataSourceFactory
   torque.dsfactory.bookstore.connection.driver = org.apache.derby.jdbc.EmbeddedDriver
   torque.dsfactory.bookstore.connection.url = jdbc:derby:bookstore
   torque.dsfactory.bookstore.connection.user = app
   torque.dsfactory.bookstore.connection.password = app
</source>

  </section>
</section>

<section>
  <title>Step 5</title>
<p>
No changes to this step are required.
</p>
</section>

<section>
  <title>Step 6</title>

<p>
I made two modifications for running the sample application:
</p>

<ul>
  <li> Set the <code>derby.system.home</code> property to the environment
       variable created in Step 3.
  </li>
  <li> Replace the mysql jar with the derby jar:
       <code>lib/derby-10.1.3.1-bin.jar</code>
  </li>
</ul>

<p>
Here is the command I ran on my Linux machine:
</p>

<source>
   [jta@gertie target]$ java -Dderby.system.home=$DERBY_SYSTEM_HOME -cp classes:lib/avalon-framework-4.1.4.jar:lib/commons-beanutils-1.7.0.jar:lib/commons-collections-3.1.jar:lib/commons-configuration-1.1.jar:lib/commons-dbcp-1.2.1.jar:lib/commons-lang-2.1.jar:lib/commons-logging-1.0.4.jar:lib/commons-pool-1.2.jar:lib/jcs-20030822.182132.jar:lib/derby-10.1.3.1-bin.jar:lib/torque-3.2.jar:lib/village-2.0.jar com.kazmier.Bookstore
</source>

</section>

<section>
  <title>Got Questions?</title>

  <p>
  Jean Anderson wrote these instructions.
  Please post any questions about them 
  to <a href="../derby_mail.html">derby-user@db.apache.org</a>.
  </p>

</section>

<p><em>Last Updated: August 17, 2006</em></p>

</body>
</document>
