You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mark Lowe <ma...@talk21.com> on 2003/07/10 13:53:05 UTC

[OT] Setting up torque with struts

This is how to configure torque to work with struts. a few weeks or  
,perhaps, months ago, Ville (vilho[at]students[dot]cc[dot]tut[dot]fi)  
after he'd spent a week bringing together all the required resources,  
he kindly mailed me some instructions on how to do so. I tested his  
instructions and got stuff running. I was waiting for him to post  
something on this but I guess he hasn't had time etc, so i thought I'd  
do it with any additional knowledge I've gained through going through  
the motions.

You can read about torque here:
http://db.apache.org/torque

...and struts here...
http://jakarta.apache.org/struts

Assumptions
I'll also assume that you're familiar with webapp development, and your  
development follows this organization:

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/appdev/index.html

..and that you've got a compliant container running etc.

In anticipation of any questions, I don't know how to configure IDE's  
if you know your IDE then you should know how to set it up. I'd  
recommend a terminal, ant an a trusty text editor.

1. Download a stable release of torque [3.02 seems to be the puppy at  
this time].

2. Untar torque and put it somewhere where you like to work. (e.g.  
~/Projects)..

3. Copy Torque's jar files to /web/WEB-INF/lib
This way all the relevant jars are in the right place, its a bit messy  
as there are several versions of some classes, I anticipated having  
problems here but none thus far.

4. Edit/Add these properties to build-torque.xml and  
[myproject]-schema.xml (see docs)

<property name="torque.output.dir" value="../src"/>
<property name="torque.doc.dir" value="../doc"/>
<property name="lib.dir" value="../web/WEB-INF/lib"/>

The paths will depend on where you prefer to organize your projects.

You can add these in build.properties.. I prefer doing it in XML, but  
I'm sure there are good reasons for using the properties file instead.

  e.g. in [myproject]-schema.xml

<database name="sparrow" package=com.sparrow.torque">

rather than defining package in a properties file, i think its more  
maintainable as a attribute of database.

Also..
make sure that your jdbc jar is specified in your  "torque-classpath"  
file set list and avoid wild carding. Both struts and torque make use  
of some of the commons sub projects, torque has a greater dependency on  
some old versions of commons-collections and such like. To get running  
I just use both together and i've had no problems as yet, I'd like to  
get the time and look through the torque source and perhaps make it  
less dependent on deprecated code.

5. Then run.

 > ant -f build-torque.xml

Hopefully the OM classes have been generated to your source directory,  
and thus can be compiled with you servlets etc.

e.g.

/src/java/com/sparrow/struts
/src/java/com/sparrow/torque
/src/sql

the following tasks fire up the sql scripts and do the DB stuff.
 > ant -f build-torque.xml create-db
 > ant -f build-torque.xml id-table-init-sql
 > ant -f build-torque.xml insert-sql

6. Now create a servlet that fires up you torque generated OM. Also  
copy the Torque.properties file here. I have a different package name  
than my OM so I can remove all the generated stuff without destroying  
my servlet and properties file. This servlet isn't anything new there  
are loads of resources on the torque site referencing this, I just  
changed the name from InitTorque because 'start' seems a perfectly good  
word for folks to read.

package com.sparrow.servlets;

import java.io.InputStream;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.*;
import javax.servlet.*;

import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
import org.apache.commons.configuration.PropertiesConfiguration;


public class StartTorque extends HttpServlet {
   public void init(ServletConfig config) throws ServletException {
	super.init(config);
	try {
	
	  InputStream configStream =  
getServletContext().getResourceAsStream(config.getInitParameter("config" 
));
	
	  PropertiesConfiguration c = new PropertiesConfiguration();
	  c.load( configStream );
	  Torque.init( c );
	
	}
	  catch (IOException e) {
	  	throw new ServletException( e.toString() );
	}
	  catch ( TorqueException e ) {
	  throw new ServletException( e.toString() );
	}
   }
}

Edit the Torque.properties file to suit your needs. This will read the  
values in Torque.properties file (covered in Torque docs).

e.g.
##i love sparrows!!!!!!
torque.database.default=sparrow
torque.database.sparrow.adapter=mysql

## Using Jdbc2Pool
torque.dsfactory.nsalliance.factory =  
org.apache.torque.dsfactory.Jdbc2PoolDataSourceFactory
torque.dsfactory. sparrow.pool.defaultMaxActive = 10
torque.dsfactory. sparrow.pool.testOnBorrow = true

##change to select * from dual for Oracle etc
torque.dsfactory. sparrow.pool.validationQuery = SELECT 1
torque.dsfactory. sparrow.connection.driver = com.mysql.jdbc.Driver
torque.dsfactory. sparrow.connection.url =  
jdbc:mysql://localhost:3306/sparrow
torque.dsfactory. sparrow.connection.user = dbuser
torque.dsfactory. sparrow.connection.password = sparrow

In web.xml

<servlet>
   <servlet-name>StartTorque</servlet-name>
   <servlet-class>com.sparrow.servlets.StartTorque</servlet-class>
   <init-param>
     <param-name>config</param-name>
      
<param-value>/WEB-INF/classes/com/sparrow/servlets/Torque.properties</ 
param-value>
   </init-param>
   <load-on-startup>3</load-on-startup>
</servlet>

7. Finally you should be able to build your whole project using your  
build file that you use for your webapp. You'll just have to import  
your OM classes into your actions and the torque docs should give you a  
clear run from here.

e.g.
import com.sparrow.torque.*;
import org.apache.torque.util.Criteria;

Again torque docs cover using criteria etc.

Notes.
If you're building an application from scratch you'll be able to  
generate your OM from your xml schema. If you're using an existing DB  
then you can generate your schema and then generate your OM. If you've  
had DBA types geeking around with outer joins there can be issues.  
Torque doesn't really support outer joins.

I'm non beard-sporting, partial to quiche and I pride myself on my low  
IQ, so please don't post me anything too clever cos i'll get confused  
start dribbling and talking about sparrows :) 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org