Home > Code > Torque

Torque

October 3rd, 2002 tony Leave a comment Go to comments

Well I’m working on a new project and I thought I would test out the Jakarta project Torque for persistence layer. The setup was pretty easy although it took me a little longer to figure out how to configure it with Resin and db pooling.

You use XML to define your objects like so:

< 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>

Once you setup your schema the coding is easy peasy:

INSERT

Book book = new Book();

book.setTitle(title);

book.setISBN(isbn);

book.save();

SELECT

Criteria crit = new Criteria();

crit.add(BookPeer.title, “Thinking in Java”);

List list = BookPeer.doSelect(crit);

There are ant tasks defined for creating the database, generating the tables, pre-populating tables, and generating your source code. It has transactional support, object and method caching, and a variety of methods of db pooling built in. It also can perform cascading deletes.

Another cool thing is that you can create criteria to use SQL joins to get an object and its foreign key objects thereby elimating multiple selects for an object.

Pretty cool. I’m anxious to see how it performs under a load.

Categories: Code Tags:
  1. Rajesh
    November 2nd, 2004 at 17:15 | #1

    how can i use a LEFT OUTER JOIN in Torque

  2. Tony Spencer
    November 3rd, 2004 at 09:53 | #2

    Rajesh,
    Torque only supports normal joins. One user went to great lengths to add LEFT and RIGHT join functionality to the Criteria. You can download his patch from this page.

    Jakarta Torque Joins Patch

    Looks like it may be rolled up into the next Torque release candidate.