Navigation trail:Torque

Getting Torque To Work

Although Torque is a great tool and remarkably effective once you get it going, many people have trouble setting it up. The documentation seems particularly weak in this area (possibly because there are so many different databases, drivers, and parameters to consider.)

This document is an attempt to help people figure out what's going wrong with their setup by listing the problems and solutions that other people have had.

PLEASE, PLEASE, PLEASE!! If you have had problems setting up Torque and solved them, add your experiences to the list. The more people who contribute, the better the document!

Installing release 3.1


Extract the zips, you will now have <your_installation_path>/torque-3.1 folder and <your_installation_path>/torque-gen-3.1 folder

Edit (3 changes primarily -- torque.project , torque.database and the database connection related setting.)


Getting Torque to generate the schema and object files

This is a listing of the typical problems people have trying to get Torque to generate their object files and schema files.

1. My problem was that I couldn't generate the java source code by running ant -f build-torque.xml. Everything else was working perfect, I got no error messages from Ant, sql code was generated and I could run that to generate my mysql database and tables and everything else. My mistake was that I wasn't setting the right name for my database schema xml file. If the project name in is "pitchers" ("torque.project = pitchers"), the schema xml file (in the schema folder) needs to be named as "pitchers-schema.xml" i.e in the format "project-schema.xml"

Might be a dumb mistake but that's what it was.


Another note on this issue---I was working from a database to XML, and was confused by the fact that when a schema is generated by from an existing database it is called schema.xml rather than xxx-schema.xml. It took me ages to work out that i had to rename to schema.xml to xxx-schema.xml, but after I had the .java files were generated correctly. Maybe the solution to this confusion is to have turbine issue a helpful warning message when it finds schema.xml but not xxx-schema.xml?



2. My problem turned out to be fairly simple to fix. When I was browsing the site I didn't find any references to what column types were allowed. I mistakenly put LONG and BOOLEAN in. This causes the build to fail without any information on what went wrong or where. After I changed my types to BIGINT and BIT everything worked correctly. Below I am including the available types that I have found.


They were found in my src/java/ file.

Hope this helps


Data types from the DTD

The above list is missing BOOLEANINT and BOOLEANCHAR according to the DTD.


another hint

if you get an error like:

[torque-sql] Couldn't read DTD specified in XML schema: [torque-sql]

check in your conf files the Doctypes. <!DOCTYPE database SYSTEM "">


Getting a Torque application to run from the command line

This is a listing of the typical problems people have trying to get their first Torque Application to run.

When you're using ant 1.4.1 you'll have this error when trying to execute jdbc ant task.

{{{ Error "Could not create task of type: torque-jdbc-transform"



Typical eror when using torque with Ant 1.4.1. You need Ant 1.5.1 to use torque.


Getting Torque to run within a web-server

This section deals with the trials and tribulations of getting your web-applications Torque-enabled.

There are several steps to help check that Torque is setup correctly with your web-application, before you start testing Criteria, Peers, etc.

  1. Make sure that you can run the Torque sample applications from the command line. MANY PEOPLE FAIL THIS STEP.

    • If you fail this step, you need to review the item on getting Torque to run on the command line. (Usually, there's a problem in the file, or libraries are missing, or something.)
  2. Write a very simple JSP file that contains a small scriplet that prints the results of "Torque.isInit()" Make sure it's true.
    • If this step fails, you need to check a number of things:
      • Make sure you are initializing Torque - many people make a TorqueInitServlet and call Torque.init() when the Servlet starts.

      • Make sure the webserver can find the file. Usually this means specify an absolute path to the file Because of the way many webservers start up, you can't guarantee what the starting directory is for the webserver. Thus, local paths to the file often fail.

      • Make sure that you've included your database driver in the WEB-INF/lib directory.
      • Make sure that you've included the appropriate Torque libraries in the WEB-INF/lib directory.

Getting Torque to run within a Junit test

This section deals with the trials and tribulations of getting your unit tests to work with Torque.

I have a base TorqueTestCase that I extend all of my test cases that use torque. I use this for any junit based tests I run. Although I have found that running Cactus style in-container tests works better for me over the long run where the container starts Torque up, but this will help you write tests if you are not in a webapp:

  1. Create a class looking like this:

{{{ public class TorqueTestCase extends TestCase {


  1. Now, admittedly the torque.props is hardcoded, but this should get you started. Just make sure all your constructors do call the <code>super( name)<code> method!

- EricPugh

Using Torque with multiple databases

This section deals with the trials and tribulations of using multiple databases with Torque.

It's fairly easy to get multiple databases working. The following examples assume you are using JNDI, since it's what I know best, but the differences are restricted to the file. Start off by choosing a handle name for each of the databases to which you will connect. In this example there will be two databases, with handles dbA and dbB. Do the following:

  1. In the schema file for dbA, the <database> element must have a name attribute with value dbA. Likewise for the dbB schema file. When the object model files are generated (by ant -f om), the name will be hard-coded in them, and cannot be overriden.

  2. In the file, create an adapter for each of your databases:
    [Note: your JNDI path may be different!]

    The key point here is the handle, emphasized in torque.database.dbA.adapter=mysql. The file handles and the handles in the schema files must correspond. This is the a fundamental issue with getting multiple databases working with Torque.

  3. Every use of the database in the code must reference a handle [this is a simplification, there is a default handle, and rules for automatic creation of a default handle, but... let's not go there]. Here is an example:

      Criteria criteria = new Criteria("dbA");
      criteria.add(SomePeer.SOME_FIELD, "some value");
    -- Gary Shea

TorqueProjectPages/GettingTorqueToWork (last edited 2009-09-20 23:04:18 by localhost)